Use of Android handler (ii)

Source: Internet
Author: User



Use of handler (ii)
The relationship between handler and thread
Handler by default, it is actually on the same thread as the activity that called it.
For example, in Example 1 of the use of handler (i), although the thread object is declared, it does not invoke the thread's start () method in the actual call, but instead calls the current thread's run () method directly.
By an example to confirm
Example 1: An Android application that creates handler and thread objects in activity, and outputs the ID and name of the current thread in the activity's OnCreate () method, Then the run method of the thread object also prints the ID and name of the current threads under output. If the result of the activity output is the same as the result of the thread object output, then it means that they are using the same thread.
Here is the activity code:


Package Android.handler;import Android.app.activity;import Android.os.bundle;import Android.os.handler;public class    Handlertwo extends activity {/** Called when the activity is first created. */handler Handler = new Handler ();        @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        The Post method is called before the layout file is set,//indicates that the contents of the layout file are not displayed until after the thread has been executed, and the thread is set to hibernate for 10 seconds,//So the final effect is to display the application main interface and wait 10 seconds before displaying the contents of the layout file             Handler.post (R);        Setcontentview (R.layout.main);        SYSTEM.OUT.PRINTLN ("Activity ID--->" +thread.currentthread (). GetId ());    System.out.println ("Activity name--->" +thread.currentthread (). GetName ()); } Runnable r = new Runnable () {public void run () {//outputs the ID of the current thread and name//if the thread ID, name and the thread ID, name of the output in the OnCreate () method above is the same ,//Then it means that they are using the same thread System.out.println ("runnable_id--->" +thread.currentthread (). GetId ()); System.out.println ("Runnable_name--->" +thread.currentthread (). GetName ()); Try{thread.Sleep (10000);//Leave thread dormant for 10 seconds}catch (interruptedexception e) {e.printstacktrace ();}} };}



Is the result of the execution:



As you can see from the results, the IDs and name of the two outputs are the same, and they use the same thread.

Now modify the code in the activity, create a thread thread, then call the thread's start () method, and then look at the output of the console.

This is just a little bit of a change to the code above.
1. Handler.post (R) is commented out first
2, add the following two lines of code is OK


        Handler.post (r);             Thread t = new Thread (r);        T.start ();

Output Result:



As you can see from this output, the ID of the thread object, name is completely different from the thread ID and name in the activity, so they are not using the same thread.

This example also conceals an effect, that is, we usually put the handler post () method after the Setcontentview (R.layout.main) This method is called, will be set up after the layout and then perform other operations, and in this example, is to place the handler post () method before the SetContent () method, and the run () method of the thread object passed in the post, and the sleep thread is executed for 10 seconds, so the effect of running the implementation will be, when the program runs, First, there is nothing on the activity, and after 10 seconds, the contents of the activity are displayed.

Ii. bundles and how to process messages in a new thread
First introduce the bundle:
Bundle It is a mapping that takes a string as a key and can be used as a value by other data types, which is equivalent to treating data as a package. In the beginner stage it can be used as a special HashMap object, but the keys and values of HashMap are object type, and the bundle's key is a string type.
Use an example to work with bundles and how to process messages in a new thread
Example 2: An Android application that prints the thread ID that the activity is currently using, then creates a new thread, uses Bundl to store the value, and then prints the ID of the threads and the values stored in the bundle.
Look at the output:




Use of Android handler (ii)

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.