Logoff and Handler

Source: Internet
Author: User

Logoff is used to process messages in the android thread. By default, a thread is not bound to any logoff. When we call lorule. Prepare (), if the current thread is not bound to any lorule, A lorule will be created to bind it to the current thread. When we call logoff. loop (), IT processes The logoff messages corresponding to the current thread, retrieves messages from the message queue, processes messages, and loops until it calls quit () function. Note: logoff. loop () is a while loop, which only calls the quit () function, loit. loop () functions can be completed, and subsequent code can be run. A thread corresponds to a loose and a loose corresponds to a message queue messagequeue. For handler, the message is actually sent to the messagequeue of its corresponding logoff message queue. The final processing is still carried out in the while loop of logoff. Loop. A logoff can be used to construct multiple handler. Because of logoff. the loop () function is a while loop that will allow the current thread to process the loop until the quit () function is called for the Logoff of the thread. Therefore, if you want to send a message to the handler or add runnable for transaction processing, either in another thread or in the runnable () Run () during transaction processing in the function. Note 1: Handler constructor handler () and handler (handler. callback callback), although there is no logoff parameter, but it actually uses logoff. mylogoff () to obtain the logoff in the current thread. NOTE 2: If the logoff you are using comes from the thread you created, you must use handler instead of the corresponding handler. getlooper (). to exit the thread. The following is a typical low.thread implementation example in the android API: Class looperthread extends thread {public handler mhandler; Public void run () {Looper. prepare (); mhandler = new handler () {public void handlemessage (Message MSG) {// process incoming messages here}; Looper. loop ();} Note: by default, the thread does not have logoff, so you need to call logoff. prepare () to create a message loop for the thread, and then pass, logoff. loop () to make the message loop take effect. In addition, the mainui thread of the activity has been created and bound to a logoff (you can see it in the main function in source code 1 ). Therefore, when creating a handler in an activity, you do not need to call logoff. Prepare () first (). For more information about the mainui thread of the activity, see the source code file activitythread. java, which is located in the source code 1 public final class activitythread under Android \ frameworks \ base \ core \ Java \ Android \ app {// omitting final applicationthread mappthread = new applicationthread (); final loginmlogint = logint. mylogoff (); Final h mh = new H ();

// Omitting private final class H extends Handler {// omitted} // omitting public static final void main (String [] args) {SamplingProfilerIntegration. start ();
Process. setArgV0 ("<pre-initialized> ");
Looper. prepareMainLooper (); if (sMainThreadHandler = null) {sMainThreadHandler = new Handler ();}
ActivityThread thread = new ActivityThread (); thread. attach (false );
If (false) {Looper. myLooper (). setMessageLogging (new LogPrinter (Log. DEBUG, "ActivityThread "));}
Logoff. loop ();
If (Process. supportsProcesses () {throw new RuntimeException ("Main thread loop unexpectedly exited ");}
Thread. detach (); String name = (thread. mInitialApplication! = Null )? Thread. mInitialApplication. getPackageName (): "<unknown>"; Slog. I (TAG, "Main thread of" + name + "is now exiting");} lifecycle functions of the Activity (such as onCreate (), onResume) it also uses a Handler (namely, the H of the ActivityThread) based on its MainUI thread logoff (that is, the Main function thread of ActivityThread)
MH Handler). That is to say, these functions are all executed in logoff. loop. Of course, if we create a logoff Handler Based on the MainUI thread of the Activity itself, the transaction processing of the Handler is also carried out in logoff. loop. We can also see this from test instance 1. Test instance 1: package com. teleca. robin; import android. app. activity; import android. OS. bundle; import android. OS. handler; import android. util. log; public class MainActivity extends Activity {/** Called when the activity is first created. * // @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); Log. I (tag, "-----------------------------------------------"); StackTraceElement st [] = Thread. currentThread (). getStackTrace (); for (int I = 0; I <st. length; I ++) Log. I (tag, I + ":" + st [I]); Log. I (tag, "----------------------------------------------"); handler = new Handler (); handler. postDelayed (r, 100); handler2.postDelayed (r, 200);} final static String tag = "robin"; Handler handler; Handler handler2 = new Handler (); runnable r = new Runnable (){
Public void run ()
{
StackTraceElement st [] = Thread. currentThread (). getStackTrace ();
For (int I = 0; I <st. length; I ++)
Log. I (tag, I + ":" + st [I]);
Log. I (tag ,"----------------------------------------------");
}
};} Test Result: 22:10:18-20. 799: INFO/robin (7844): ------------------------------------------------- 07-20 22:10:18. 819: INFO/robin (7844): 0: dalvik. system. VMStack. getThreadStackTrace (Native Method) 07-20 22:10:18. 819: INFO/robin (7844): 1: java. lang. thread. getStackTrace (Thread. java: 788) 07-20 22:10:18. 819: INFO/robin (7844): 2: com. teleca. robin. mainActivity. onCreate (MainActivity. java: 15) 07-20 22:10:18. 819: INFO/ Robin (7844): 3: android. app. instrumentation. callActivityOnCreate (Instrumentation. java: 1047) 07-20 22:10:18. 819: INFO/robin (7844): 4: android. app. activityThread. extends mlaunchactivity (ActivityThread. java: 2633) 07-20 22:10:18. 819: INFO/robin (7844): 5: android. app. activityThread. handleLaunchActivity (ActivityThread. java: 2685) 07-20 22:10:18. 819: INFO/robin (7844): 6: android. app. activityThread. access $2300 (Acti VityThread. java: 126) 07-20 22:10:18. 819: INFO/robin (7844): 7: android. app. activityThread $ H. handleMessage (ActivityThread. java: 2038) 07-20 22:10:18. 819: INFO/robin (7844): 8: android. OS. handler. dispatchMessage (Handler. java: 99) 07-20 22:10:18. 819: INFO/robin (7844): 9: android. OS. logoff. loop (logoff. java: 123) 07-20 22:10:18. 819: INFO/robin (7844): 10: android. app. activityThread. main (ActivityThread. java: 4633) 07-2 0 22:10:18. 819: INFO/robin (7844): 11: java. lang. reflect. method. invokeNative (Native Method) 07-20 22:10:18. 819: INFO/robin (7844): 12: java. lang. reflect. method. invoke (Method. java: 521) 07-20 22:10:18. 819: INFO/robin (7844): 13: com. android. internal. OS. zygoteInit $ MethodAndArgsCaller. run (ZygoteInit. java: 858) 07-20 22:10:18. 819: INFO/robin (7844): 14: com. android. internal. OS. zygoteInit. main (ZygoteInit. java: 616) 0 7-20 22:10:18. 819: INFO/robin (7844): 15: dalvik. system. nativeStart. main (Native Method) 07-20 22:10:18. 819: INFO/robin (7844): -------------------------------------------------- 07-20 22:10:18. 969: INFO/robin (7844): 0: dalvik. system. VMStack. getThreadStackTrace (Native Method) 07-20 22:10:18. 969: INFO/robin (7844): 1: java. lang. thread. getStackTrace (Thread. java: 788) 07-20 22:10:18. 969: INFO/robin (7844): 2: com. te Leca. robin. mainActivity $ 1.run( MainActivity. java: 29) 07-20 22:10:18. 969: INFO/robin (7844): 3: android. OS. handler. handleCallback (Handler. java: 587) 07-20 22:10:18. 969: INFO/robin (7844): 4: android. OS. handler. dispatchMessage (Handler. java: 92) 07-20 22:10:18. 969: INFO/robin (7844): 5: android. OS. logoff. loop (logoff. java: 123) 07-20 22:10:18. 969: INFO/robin (7844): 6: android. app. activityThread. main (ActivityThread. j Ava: 4633) 07-20 22:10:18. 969: INFO/robin (7844): 7: java. lang. reflect. method. invokeNative (Native Method) 07-20 22:10:18. 969: INFO/robin (7844): 8: java. lang. reflect. method. invoke (Method. java: 521) 07-20 22:10:18. 969: INFO/robin (7844): 9: com. android. internal. OS. zygoteInit $ MethodAndArgsCaller. run (ZygoteInit. java: 858) 07-20 22:10:18. 969: INFO/robin (7844): 10: com. android. internal. OS. zygoteInit. main (ZygoteInit. Java: 616) 07-20 22:10:18. 969: INFO/robin (7844): 11: dalvik. system. nativeStart. main (Native Method) 07-20 22:10:18. 969: INFO/robin (7844): ------------------------------------------------ 07-20 22:10:19. 019: INFO/robin (7844): 0: dalvik. system. VMStack. getThreadStackTrace (Native Method) 07-20 22:10:19. 019: INFO/robin (7844): 1: java. lang. thread. getStackTrace (Thread. java: 788) 07-20 22:10:19. 019: INFO/robin (7844 ): 2: com. teleca. robin. mainActivity $ 1.run( MainActivity. java: 29) 07-20 22:10:19. 019: INFO/robin (7844): 3: android. OS. handler. handleCallback (Handler. java: 587) 07-20 22:10:19. 019: INFO/robin (7844): 4: android. OS. handler. dispatchMessage (Handler. java: 92) 07-20 22:10:19. 019: INFO/robin (7844): 5: android. OS. logoff. loop (logoff. java: 123) 07-20 22:10:19. 019: INFO/robin (7844): 6: android. app. activityThread. main (Activity Thread. java: 4633) 07-20 22:10:19. 019: INFO/robin (7844): 7: java. lang. reflect. method. invokeNative (Native Method) 07-20 22:10:19. 019: INFO/robin (7844): 8: java. lang. reflect. method. invoke (Method. java: 521) 07-20 22:10:19. 019: INFO/robin (7844): 9: com. android. internal. OS. zygoteInit $ MethodAndArgsCaller. run (ZygoteInit. java: 858) 07-20 22:10:19. 019: INFO/robin (7844): 10: com. android. internal. OS. zygoteInit. main (Zyg OteInit. java: 616) 07-20 22:10:19. 019: INFO/robin (7844): 11: dalvik. system. nativeStart. main (Native Method) 07-20 22:10:19. 019: INFO/robin (7844): ------------------------------------------------ check ActivityThread. java source code. You can see that the system will send messages to the Handler through ApplicationThread mAppThread for transaction processing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.