Fun Handler and fun Handler
Let's take a look at a simple message first.
privateProgressBar mProgressBar;
privateint i =0;
PrivateHandler mHandler = newHandler () {// create a Handle
@Override
publicvoid handleMessage(Message msg){
super.handleMessage(msg);
Log.i("-mHandler->",i+"");
mProgressBar.setProgress(i);
}
};
privateRunnable runnable =newRunnable(){
@Override
publicvoid run(){
Log.i("-Runnable->",i+"");
i +=10;
// The task to be done. Call this Runnable object again to implement the timer operation every two seconds.
MHandler. postDelayed (runnable, 2000); // Timer
MHandler. sendMessageDelayed (Message. obtain (), 0); // send a Message to trigger the override handleMessage Method
}
};
@Override
protectedvoid onCreate(@NullableBundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_handler);
mProgressBar =(ProgressBar) findViewById(R.id.pb_handler);
MHandler. post (runnable); // start the execution thread
// Runnable. run (); // you can use this to start a thread.
}
/**
* Stop the thread operation during deletion.
*/
@Override
protectedvoid onDestroy(){
super.onDestroy();
mHandler.removeCallbacks(runnable);
}
}
Two methods for obtaining Message
// Two methods for obtaining Message
// Returns a new Message from the global message pool.
MHandler. obtainMessage () and Message. obtain ()
// The two methods are actually the same, but one is obtained through Handler and the other is obtained through the static method of Message. When you query the Handler source code, you will find that the obtainMessage method is composed:
publicfinalMessage obtainMessage()
{
returnMessage.obtain(this);
}
After querying the source code, it is found that the Message provides the following variables:
Message recycleUnchecked () method
void recycleUnchecked(){
// Mark the message as in use while it remains in the recycled object pool.
// Clear out all other details.
flags = FLAG_IN_USE; //int
what =0; // int
arg1 =0; // int
arg2 =0; // int
obj =null; // Object
ReplyTo = null; // Messenger, Messenger
sendingUid =-1;
when =0; // long
target =null; // Handler
callback =null; // Runable
data =null; // Bundle
synchronized(sPoolSync){
if(sPoolSize < MAX_POOL_SIZE){
next = sPool;
sPool =this;
sPoolSize++;
}
}
}
The advantage of using system variables is that the consumption of the system can be greatly reduced. Therefore, the code for updating the progress bar should be modified
mProgressBar.setProgress(msg.arg1);
privateRunnable runnable =newRunnable(){
@Override
publicvoid run(){
Log.i("-Runnable->", i +"");
i +=3;
// The task to be done. Call this Runnable object again to implement the timer operation every two seconds.
mHandler.postDelayed(runnable,300);
Message msg = mHandler.obtainMessage();
msg.arg1 +=i;
MHandler. sendMessage (msg); // The handleMessage method that will be overwritten only when a message is sent
// MHandler. sendMessageDelayed (Message. obtain (), 0); // The handleMessage method to be rewritten is triggered when a Message is sent.
}
};
Handler optimized
privateHandler mHandler =newHandler(){
@Override
publicvoid handleMessage(Message msg){
super.handleMessage(msg);
//Log.i("-mHandler->", i + "");
if(msg.arg1 >100){
mHandler.removeCallbacks(runnable);
}else{
mProgressBar.setProgress(msg.arg1);
}
}
};
From Weizhi note (Wiz)