我有一個活動(myActivity)和一個線程(MyThread),兩者都帶有一個允許我在UI線程和myCustomThread之間發送消息的處理程序。 現在我想定期(10秒)從MyActivity中調用MyThread線程的ALIVE消息。我怎麼能做到這一點?線程中的定期消息
MyActivity:
public void onResume()
{
super.onResume();
this.thread = new MyThread(activityHandler);
this.threadHandler = this.thread.gethandler();
threadMessage = this.threadHandler.obtainMessage();
threadMessage.what = AUTH;
this.threadHandler.sendMessage(threadMessage);
}
MyThread的:
@覆蓋
public void run()
{
Looper.prepare();
this.threadHandler = initHandler();
this.message = this.activityHandler.obtainMessage();
this.message.what = CONNECTED;
activityHandler.sendMessage(this.message);
Looper.loop();
}
private Handler initHandler()
{
return new Handler() {
public void handleMessage(Message msg)
{
switch(msg.what)
{
case AUTH :
{
auth();
break;
}
case ALIVE :
{
sendAlive();
break;
}
}
}
};
}
感謝您的幫助
延遲發佈是一個很好的解決方案。我使用它沒有計時器(見我以前的答案),它的效果很好。謝謝 – grunk