2013年7月21日星期日

In Activity creates a handler, but the handler to take the thread ID is not obtained when the Activity ID number, ask why.

code is as follows:

 
public class MainActivity extends Activity implements Runnable {
Handler handler = new Handler () { public void handleMessage (Message msg) { Sp (Thread.currentThread (). GetId () + "------------- Handler"); }
}; @ Override
public void onCreate (Bundle savedInstanceState) { super.onCreate (savedInstanceState); Sp (Thread.currentThread (). GetId () + "------------- MainActivity"); new Thread (this). start (); }
@ Override
public void run () {
Sp (Thread.currentThread (). GetId () + "------------- New Thread"); try { Thread.sleep (3000); } Catch (InterruptedException e) { e.printStackTrace (); }
handler.handleMessage (handler.obtainMessage ()); }
}

------ Solution ------------------------------------ --------------------


not looking not seen it yet :-)

You missed handler.handleMessage (handler.obtainMessage ());
changed handler.sendMessage (handler.obtainMessage ());


otherwise equivalent subroutine call.


------ For reference only ---------------------------------- ----------------------
top.
------ For reference only -------------------------------------- ------------------
main thread has its own message queue, the general thread is created without its own message queue, the message processing is done in the main thread, if thread using Looper.prepare () and Looper.loop () creates a message queue can allow message processing in this thread.

 

public class MainActivity extends Activity implements Runnable { Handler handler = new Handler () { public void handleMessage (Message msg) { Sp (Thread.currentThread (). GetId () + "---- --------- Handler "); / / This is the main thread ID
}
}; @ Override
public void onCreate (Bundle savedInstanceState) { super.onCreate (savedInstanceState); Sp (Thread.currentThread (). GetId () + "------------- MainActivity"); ;/ / This is the main thread ID
new Thread (this). start (); }
@ Override
public void run () { Sp (Thread.currentThread (). GetId () + "------------- New Thread"); ; / / This is the new thread ID
try { Thread.sleep (3000); } Catch (InterruptedException e) { e.printStackTrace (); }
handler.handleMessage (handler.obtainMessage ()); }
}



------ For reference only ---------------------------------- ----------------------


Thank you for your answer.

I run this program, the result is this:
 
08-28 02:51:13.838: INFO / System.out (437): 1 ------------- MainActivity
08-28 02:51:14.248: INFO / System.out (437): 8 ------------- New Thread
08-28 02:51:17.317: INFO / System.out (437): 8 ------------- Handler


I do not know why, and the child thread handler get the same ID, please enlighten.
------ For reference only -------------------------------------- ------------------


Thank you, solved.
------ For reference only -------------------------------------- ------------------

access points :-)

------ For reference only ---------------------------------- ----------------------
 
  
------ For reference only ------------------------ --------------------------------
Thank you to share it with those photos
--- ............ --- For reference only -------------------------------------------- ------------
incisive, fontlose Daniel

没有评论:

发表评论