Android 物件導向/事件驅動 thread透過handler.sendMessage 來驅動 handler 修改 GUI 的狀態

Android 物件導向/事件驅動 thread透過handler.sendMessage 來驅動 handler 修改 GUI 的狀態

Android 物件導向/事件驅動 thread透過handler.sendMessage 來驅動 handler 修改 GUI 的狀態

 

資料來源:

http://givemepass.blogspot.tw/2013/09/threadhandlerhandlerpost.html

 

http://cloudflyair.blogspot.tw/2014/08/androidhandlermemory-leak.html

 

 

 

private MyHandler mHandler = new MyHandler();
private Thread mThread = new Thread(new Runnable() {
public void run() {
Log.e("ThreadID", Long.toString(Thread.currentThread().getId()));
Log.e("ThreadName", Thread.currentThread().getName());
Message msg = new Message();
msg.what = 0;
mHandler.sendMessage(msg);
}
});
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mThread.start();//啟動執行序
Log.e("ActivityThreadID",
Long.toString(Thread.currentThread().getId()));
Log.e("ActivityThreadName",Thread.currentThread().getName());
}
class MyHandler extends Handler
{
@Override
public void handleMessage(Message msg)
{
switch (msg.what)
{
case 0:
// 在這邊寫要操作UI(Activity)的部分,就會動作了。
Log.e("HandlerThreadID", Long.toString(Thread.currentThread().getId()));
Log.e("HandlerThreadName", Thread.currentThread().getName());
break;
}
super.handleMessage(msg);
}
}
 

 

 


發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *