當天的第二個問題! (對不起,新手)是mHandler.obtainMessage應該觸發handleMessage(消息消息)
我想創建一個單獨的線程,並在該線程中我需要能夠發送信息回到UI線程。對於這一點,我說幹就幹,創建一個新的處理類:
private static class nHandler extends Handler{
private final WeakReference<MainActivity> mTarget;
public nHandler(MainActivity context){
mTarget = new WeakReference<MainActivity>((MainActivity) context);
}
@Override
public void handleMessage(Message msg){ Log.d("Something", "It got here")}
}
在我的主要活動課我有一個私人靜態nHandler mHandler;然後,我使用mHandler = new nHandler(this)在我的onCreate()中創建了一個處理程序;
當我創建我的其他線程,並且它運行時,我創建一個處理程序對象並將其設置爲我在構造函數中傳遞的對象。在這個其他類我然後有一個功能:
private synchronized void setState(int state){
//lets add some logging
mState = state;
//Give the handler something so the Main Activity Updates
mHandler.obtainMessage(MainActivity.HANDLER_CHANGE_SERVER_STATUS, state, -1);
}
但在日誌中我沒有看到我的日誌出現。我可能不完全理解處理程序應該如何工作,所以如果你認爲這種情況可以幫助我理解它們並使其正常工作?
謝謝!