Android Handler usage (2)

Source: Internet
Author: User
Handler usage (2)

I. Relationship between Handler and thread

By default, Handler is in the same thread as the Activity that calls it.
For example, in Example 1 of Handler's use (1), although the thread object is declared, it does not call the start () method of the thread in actual calls, instead, it directly calls the run () method of the current thread.

Let's use an example to confirm

Example 1: an Android Application creates Handler and thread objects in the Activity, and outputs the id and name of the current thread in the onCreate () method of the Activity, then, the id and name of the current thread are printed in the run method of the thread object. If Activity output results are the same as the output results of the thread object, they use the same thread.
The following is the Activity code:

Package eoe. demo;

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 );
// Call the post method before setting the layout file,
// Indicates that the content in the layout file is displayed after the thread is executed, and the thread is set to sleep for 10 seconds,
// The final result is that the main interface of the application is displayed first, and the content in the layout file is displayed 10 seconds later.
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 (){
// Output the id and name of the current thread
// If the output thread id and name are the same as the output thread id and name in the onCreate () method above,
// It indicates that they use the same thread.
System. out. println ("runnable_id --->" + Thread. currentThread (). getId ());
System. out. println ("runnable_name --->" + Thread. currentThread (). getName ());
Try {
Thread. sleep (10000); // sleep the Thread for 10 seconds
} Catch (InterruptedException e ){
E. printStackTrace ();
}
}
};
}

Is the result of execution:

The result shows that the two outputs have the same id and name, and they use the same thread.

Modify the code in the Activity, create a Thread, call the start () method of the Thread, and observe the output result of the console.

Here, you only need to slightly modify the above Code.
1. Comment out handler. post (r) first.
2. Add the following code and you will be OK.

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

Output result:

From this output, we can see that the id and name of the thread object are completely different from the thread id and name in the activity. Therefore, they are not using the same thread.

This example also hides an effect, that is, we usually put the post () method of Handler in setContentView (R. layout. main) This method is called after the layout is set and other operations are performed. In this example, the post () method of Handler is called before the setContent () method, the run () method of the thread object passed in post executes the sleep thread for 10 seconds, so the effect of running the implementation is that after the program runs, first, there is no content in the Activity. After 10 seconds, the content in the Activity will be displayed.

Ii. Bundle and how to process messages in a new thread
First introduce Bundle:

Bundle is a mapping that uses string as the key and other data types as values. It is equivalent to treating data as a package. In the beginner's stage, we can regard it as a special HashMap Object. However, the keys and values of HashMap are of the Object type, while those of Bundle are of the String type.

An example is used to describe Bundle and how to process messages in a new thread.
Example 2: An Android Application prints the thread id currently used by the Activity, creates a new thread, and uses the Bundl to store the value, finally, print the thread id and the value stored in the Bundle.
Let's take a look at the output:

Related Article

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.