關於我 : Kun-hsien(KH)
研究&開發是一段艱難的旅程:享受在資料字串與信號間遊走。
從VB->HTML->PHP->MySQL->Fedora->MCSE->JAVA->XML->Android->Embeded C->C ->C# ->Python-> Arduino -> IoT ->MQTT ->Spark ->CAN ->I2C....
這裡除了紀錄開發Apps遇到問題的Solution並分享給大家外,各種開發技術的文章也在此探討!希望大家都能順利解決自己遇到的問題!
2014年1月5日 星期日
[Android]一個具UI即時更新thread的樣板&小時鐘範例
public class Clock extends Activity {
protected static final int CLOCK_UPDATE = 0x101; //設定Clock update記憶體存放的位置
ImageView Isecond1,Isecond0,Ihour,Iminute;
float x1,x2;
Handler clockHandler = new Handler() { //設定一handler 並把時間更新的方法case到以下
public void handleMessage(Message msg) {
int Zsecond = Integer.parseInt(Jsecond());
int Zhour = Integer.parseInt(Jhour());
int Zminute = Integer.parseInt(Jminute());
switch (msg.what) {
case ActivityTwo.CLOCK_UPDATE:
switch(Zsecond){ //case秒
case 0:
略
}
switch(Zhour){ //case小時
case 0:
略
}
switch(Zminute){ //case分
case 0:
略
}
break;
}
super.handleMessage(msg); 繼承父類別handleMessage的方法去遞迴更新時間
}
};
@Override
public void onCreate(Bundle savedInstanceState) { //產生的畫面並在此宣告UI的元件
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_two);
Isecond1 = (ImageView) findViewById(R.id.Isecond1);
Isecond0 = (ImageView) findViewById(R.id.Isecond0);
Ihour = (ImageView) findViewById(R.id.Ihour);
Iminute = (ImageView) findViewById(R.id.Iminute);
new Thread(new clockThread()).start(); //在onCreate去new一個clockThread並start他
}
public String Jhour() {
Time time = new Time();
time.setToNow();
return String.format("%02d",time.hour);
}
public String Jminute(){
Time time = new Time();
time.setToNow();
return String.format("%02d",time.minute);
}
public String Jsecond(){
Time time2 = new Time();
time2.setToNow();
return String.format("%02d",time2.second);
}
class clockThread implements Runnable {
public void run() {
while (!Thread.currentThread().isInterrupted()) {
Message message = new Message();
message.what = Clock.CLOCK_UPDATE;
Clock.this.clockHandler.sendMessage(message);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
}
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言