//HandlerThread 就把它想成個球場
private HandlerThread mHandlerThread;
Context mContext;
@Override
protected void onCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext=this;
// 蓋一個球場並取名稱
mHandlerThread = new HandlerThread("Deyu");
// 球場開張
mHandlerThread.start();
//Handler就當做是球員 把他丟進球場裡玩
Handler myhandler = new Handler(mHandlerThread.getLooper());
//對球員下testRunnable命令,你就是總教頭
myhandler.post(testRunnable);
}
//Runnable就是命令,不可抗旨,命令從run開始
private Runnable testRunnable = new Runnable(){
@Override
public void run(){
// TODO Auto-generated method stub
Log.e("What Thread",Thread.currentThread().getName());
Toast.makeText(mContext, "Go and do task in "+Thread.currentThread().getName(), Toast.LENGTH_SHORT).show();
}
};
@Override
protected void onStop(){
// TODO Auto-generated method stub
super.onStop();
//結束記得跟他說球賽結束
myhandler.removeCallbacks(testRunnable);
//結束記得球場用不到要猜掉 地可以給別人用齁
mHandlerThread.interrupt();
mHandlerThread=null;
}
留言列表