Handle Classes and Threads

Source: Internet
Author: User
Tags message queue

  First declare the handle object and the Handlemessage method of the class:

Handler Barhandler = new Handler () {

@Override
public void Handlemessage (Message msg) {
TODO auto-generated Method Stub
Super.handlemessage (msg);
Firstbar.setprogress (MSG.ARG1); Update progress bar Progress
}
}; Declaring handle objects

  Then declare the Runnable object, press the Runnable object into the queue via the handler object (first press the Barhandler.post (updatathread);) in the listener yourself:

Runnable updatathread = new Runnable () {
int i = 0;
@Override
public void Run () {
TODO auto-generated Method Stub
i+=10;
Get a Message object, the message class is provided by the Android operating system
Message msg = Barhandler.obtainmessage ();
Set the value of the arg1 parameter of the MSG object to I, passing messages with the two member variables arg1 and arg2, with the advantage that system performance is less expensive
MSG.ARG1 = i;
try{
Thread.Sleep (1000);
}catch (Interruptedexception e) {
E.printstacktrace ();
}
Presses the message into the message queue, the current thread continues to execute down, and the SendMessage method asynchronously calls the Handlemessage method after it finishes executing
Barhandler.sendmessage (msg);
if (i = = Firstbar.getmax ()) {//When the value of I equals the current progress, the Updatathread object is removed from the queue
Barhandler.removecallbacks (Updatathread);
}
else{
The thread is pressed into the thread queue, and the Updatathread thread and the main thread of this method call
Barhandler.post (Updatathread);
Another method is to use the thread object's Start method to call the threads that are not on the same thread as the main threads
Thread t = new thread (updatathread);
T.start ();
}
}
};

Handle Classes and Threads

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.