Summary of Handler

Source: Internet
Author: User

Summary of Handler

We created the service. Activity,broadcast is a primary thread processing, that is, the UI thread,

However, for time-consuming operations, such as large files for I/O reading and writing, database operations and network downloads take a long time, in order not to block the user interface, the ANR Response Prompt window, we can consider using thread threads to solve

1. For a thread to refresh a view-based interface, you can use the Postinvalidate () method to process in-thread, which also provides some overriding methods such as postinvalidate (int left,int

Top,int Right,int bottom) to refresh a hold area, and delay operations such as postinvalidatedelayed (long Delaymiliseconds) or postinvalidatedelayed (long delaymiliseconds,int left,int top,int Right,int Bottom) method, where the first parameter is milliseconds.

2. It is recommended to use a handler to handle these, you can call the handler object in a thread's run () method of a PostMessage or SendMessage method to implement, the Android program maintains a message queue, will take turns to deal with these,

3.Looper: In fact, every thread in Android follows a looper,looper to help thread maintain a message queue, but Looper and handler have nothing to do with it. We can see from the open source code that Android also provides a thread inheriting class Handlerthread can help us handle, in the Handlerthread object can get Looper object control handle through Getlooper, We can map this Looper object to a handler to implement a thread synchronization mechanism, Looper object execution needs to initialize the Looper.perpare method, but also to release resources, using the Looper.release method.

4.Massage: For Android handler can pass some content, through the bundle object can encapsulate String,integer and blob binary object, We pass a bundle object to the handle processor by using handler in the thread to absorb the high Sendemptymessage or SendMessage method. For the handler class provides an overriding method Handlermessage (Message msg) to judge through Msg. What to differentiate each piece of information. The bundle is unpacked to implement the handler class to update the contents of the UI thread to implement the refresh operation of the control. Related handler objects related to sending sendxxx related methods are as follows, but also postxxxx related methods, one for sending directly after the return, one for processing before returning.

5. A task and Asynctask processing method is also available on Android

6. Threads:

New Thread (

New Runabler () {

public void Run () {

View.invalidate ();

}

}

). Start ();

7.thread+handler:

Handler myhandler=new Handler () {

public void Handlemessage (Message msg) {

Switch (msg.what) {

Case Testhadler.guiupdateidentufier:

View.invalidate ();

}

}

Super.handlermessgae (msg);

};

Class MyThread Implement runable{

public void Run () {

while (! Thread.CurrentThread (). isinterrupted ()) {

Message messgae=new message ();

Message.what=testhandler.guiupdateidentufier;

TestHandler.this.myHandler.sentMessage (message);

Try (thread.sleep (100);) {

catch () {}

}

}

}

}

8.timertask+handler:

public class Testtimer extends Activity () {

Timer timer=new timer ();

Handler handler=new Handler () {

public void Handlemessage (Message msg) {

Switch (msg.what) {

Case 1:settitle ("123");

Break

}

Super.handlemessage (msg);

}

};

TimerTask task=new TimerTask () {

public void Run () {

Message msg=new message ();

Msg.what=1;

Handler.senmessage (msg);

}

};

public void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinsancestate);

Setcontenview (R.layout.main);

Timer.schedule (task,10000);

}

}

9.runable+handler.postdelayed (Runable,time): Regularly updates the UI in Android, usually using Java.util.Timer, Java.util.TimerTask, Android.os.Handler combination, in fact Handler itself has provided the function of timing.

Privae Handler handler=new Handler ();

Priavte runable myrunable=new runable () {

public void Run () {

if (run) {

Handler.postdelayed (this,1000);

count++;

}

Tvcounter.settext ("Count:" +count):

}

};

And then call it somewhere else:

Handler.post (myrunable);

Handler.post (Myrunable,time);

Case VIEW: http://shaobin0604.javaeye.com/blog/515820

Summary of Handler

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.