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