Several ways to manipulate the UI for the Android development _android

Source: Internet
Author: User

There are often problems with Android projects that update the UI after time-consuming operations are completed in a child thread, and then summarize the updated approach to some of the items that you've been experiencing:

Before you look at the method, you need to know about the messaging mechanism in Android.

Method 1 Activity.runonuithread

The method is as follows:

Runonuithread (New Runnable () { 
@Override public 
void Run () { 
tv.settext ("Hello"); 
} 

This approach is easy to use, and if the current thread is a UI thread, then the action is executed immediately. If the current thread is not a UI thread, the UI thread is published to the event queue. In fact, similar to handler, it is the request message that updates the UI, joins the event queue, waits for the main thread to execute when it is idle.

Method 2 Handler

The main thread is defined handler as follows:

Handler Mhandler = new Handler () { 
@Override public 
void Handlemessage (msg) { 
super.handlemessage (msg); 
Switch (msg.what) {case 
0: 
String data = (string) msg.obj; 
Textview.settext (data); 
break; 
Default: Break 
; 
} 
} 

The child thread sends a message informing handler that the UI update is complete, as follows:

New Thread (New Runnable () { 
@Override public 
void Run () { 
//time consuming Operation 
mhandler.sendemptymessage (0); 
Message msg =new message (); 
Msg.obj = "data";//Can be a basic type, can be an object, can be a list, map, and other 
mhandler.sendmessage (msg); 
 

Method 3 View.post

Final Button btn = (Button) Findviewbyid (R.ID.BTN); 
Btn.post (New Runnable () { 
@Override 
publicvoid Run () { 
btn.settext ("Hello"); 
} 

The above code is to update the content in BTN, the same as the following code can achieve this effect.

Handler Handler = new Handler (); 
Final Button btn = (Button) Findviewbyid (R.ID.BTN); 
Handler.post (New Runnable () { 
@Override public 
void Run () { 
btn.settext ("Hello"); 
} 

This is done using the Handler.post method, one is the View.post method, the Handler.post method has been introduced in the Android message mechanism, in fact, it is also called Method 2 in the Send method.

Now look at the source code for the View.post method:

public Boolean post (Runnable action) { 
Handler Handler; 
Attachinfo attachinfo = mattachinfo; 
if (attachinfo!= null) { 
handler = Attachinfo.mhandler; 
} else { 
//Assume that post'll succeed later 
V Iewrootimpl.getrunqueue (). Post (action); 
return true; 
} 
Return Handler.post (action); 

The main functional code in the method is Attachinfo.mhandler, get the hanlder of the current thread (that is, the UI thread), and post the action object into handler. In the handler process, the link text has been analyzed so clearly that it wraps the action object that is passed over into a message (the callback action) and then puts it into the UI thread's messaging loop. In handler's DispatchMessage method, the first sentence is set for it, directly calling the Runnable run method. At this point, it has been routed to the UI thread, so we can update the UI without any hesitation.

Method 4 Broadcast

Sends broadcasts in a child thread, receives broadcasts in the main thread, and updates the UI.

Method 5 Using Asynctask

To simplify access to the UI in child threads, the system provides us with Asynctask.

Asynctask is a lightweight asynchronous task class that can perform background tasks in a thread pool and then pass execution progress and results to the main thread and update the UI. Essentially, Asynctask encapsulates thread and handler, but Asynctask is not suitable for specially time-consuming background tasks, and it is recommended that you use a thread pool if you need to perform a particularly time-consuming task.

Different API versions of Asynctask have different performance, so you need to be aware of them. In order to control space, the use of specific asynctask methods and working principles, I intend to be written in a separate description.

The above is a small set to introduce the Android development of the child threading operation UI Several methods, I hope to help you, if you have any questions welcome to my message, small series will promptly reply to everyone!

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.