Android implements methods for updating the UI in the activity in child threads _android

Source: Internet
Author: User
Tags thread class

The examples in this article describe how Android implements the UI in a child thread to update the activity. Share to everyone for your reference, specific as follows:

Under the Android platform, when multithreaded programming, it is often necessary to do some processing in a separate thread outside the main threads, and then update the user interface display. However, the problem with directly updating the page in a thread other than the main thread is that the system will report this exception:

Error/androidruntime (1222): Android.view.viewroot$calledfromwrongthreadexception:only The original thread that Created a view hierarchy can touch their views.

Perhaps the programmer will call the Toast.maketext () method in the thread, trying to display some hint information in the UI, which will also be reported as an error:

Can ' t create handler inside thread that has not called looper.prepare ()

Workaround: The UI in the activity cannot be updated directly in a child thread, typically by a child thread passing messages to the activity, and then the activity updates the UI on its own. There's a class called Android.os.Handler in Android that's used to do this.

1. Declare a variable of a Android.os.Handler class in an activity that needs to be updated by the thread.

Private Handler Handler;

2. Add the initialization of the handler in the OnCreate function:

@Override public
void OnCreate (Bundle savedinstancestate) {
//other code ...
.....
..... Handler=new handler () {public
void Handlemessage (msg) {
string message= (String) msg.obj;// obj is not necessarily a string class, it can be another class, look at the user's specific application
 ///change the main thread's UI according to the information in the message//...                           }}
;

In addition, a handler get function needs to be provided in the activity so that the thread can obtain handler and then pass the message.

Public Handler GetHandler () {return
this.handler;
}

3. The child-thread class needs to hold a context-class object representing the contexts in which the reference is directed to the activity object to which the UI is updated, generally declared as:

Private context CTX;

CTX is then initialized in the child thread class constructor or other function in order to get the handler object in the Activity object. (or in other ways, as long as the child thread can get the handler object in the activity.) )

4. In the final step, when a child thread runs to a place where a message needs to be delivered to the activity, an object of the Android.os.Message class is created, the object to be transferred is added to the messages, and the main thread is routed through the handler publication, as follows:

String str_temp= "message to be passed to the main thread"
= Message.obtain ();
Message.obj=str_temp;
Deliver messages via handler, handler
handler.sendmessage (message);

Remember, the handler here is the same object as the handler in the activity Oh, this is the message sent to the activity.

In addition, this method not only allows the child thread to update the UI, but can also be used for a different purpose. Now we assume that the child thread might throw some errors, which should be normal, so how can you let the error message be known to the user? Quite simply, in a catch statement block, the catch to the wrong object is placed in the message.obj and passed to the activity,activity to display the error message using the Toast.maketext () method.

For more information on Android-related content readers can view the site: "The Android thread and message mechanism usage Summary", "Android programming activity Operation Skills Summary", "Android debugging techniques and common problems solution summary", " Android Development Primer and Advanced tutorials, Android Multimedia how-to summary (audio, video, recording, etc), summary of Android Basic components usage, Android View view tips, Android layout layout tips and A summary of the usage of Android controls

I hope this article will help you with the Android program.

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.