Fun Handler and fun Handler

Source: Internet
Author: User

Fun Handler and fun Handler
 

  • Let's take a look at a simple message first.
    1. privateProgressBar mProgressBar;
    2. privateint i =0;
    3. PrivateHandler mHandler = newHandler () {// create a Handle
    4. @Override
    5. publicvoid handleMessage(Message msg){
    6. super.handleMessage(msg);
    7. Log.i("-mHandler->",i+"");
    8. mProgressBar.setProgress(i);
    9. }
    10. };
    11. privateRunnable runnable =newRunnable(){
    12. @Override
    13. publicvoid run(){
    14. Log.i("-Runnable->",i+"");
    15. i +=10;
    16. // The task to be done. Call this Runnable object again to implement the timer operation every two seconds.
    17. MHandler. postDelayed (runnable, 2000); // Timer
    18. MHandler. sendMessageDelayed (Message. obtain (), 0); // send a Message to trigger the override handleMessage Method
    19. }
    20. };
    21. @Override
    22. protectedvoid onCreate(@NullableBundle savedInstanceState){
    23. super.onCreate(savedInstanceState);
    24. setContentView(R.layout.activity_handler);
    25. mProgressBar =(ProgressBar) findViewById(R.id.pb_handler);
    26. MHandler. post (runnable); // start the execution thread
    27. // Runnable. run (); // you can use this to start a thread.
    28. }
    29. /**
    30. * Stop the thread operation during deletion.
    31. */
    32. @Override
    33. protectedvoid onDestroy(){
    34. super.onDestroy();
    35. mHandler.removeCallbacks(runnable);
    36. }
    37. }
     
  • Two methods for obtaining Message
    1. // Two methods for obtaining Message
    2. // Returns a new Message from the global message pool.
    3. MHandler. obtainMessage () and Message. obtain ()
    1. // 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:
    2. publicfinalMessage obtainMessage()
    3. {
    4. returnMessage.obtain(this);
    5. }
  • After querying the source code, it is found that the Message provides the following variables:
    1. Message recycleUnchecked () method
    2. void recycleUnchecked(){
    3. // Mark the message as in use while it remains in the recycled object pool.
    4. // Clear out all other details.
    5. flags = FLAG_IN_USE; //int
    6. what =0; // int
    7. arg1 =0; // int
    8. arg2 =0; // int
    9. obj =null; // Object
    10. ReplyTo = null; // Messenger, Messenger
    11. sendingUid =-1;
    12. when =0; // long
    13. target =null; // Handler
    14. callback =null; // Runable
    15. data =null; // Bundle
    16. synchronized(sPoolSync){
    17. if(sPoolSize < MAX_POOL_SIZE){
    18. next = sPool;
    19. sPool =this;
    20. sPoolSize++;
    21. }
    22. }
    23. }
    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
    1. mProgressBar.setProgress(msg.arg1);
    1. privateRunnable runnable =newRunnable(){
    2. @Override
    3. publicvoid run(){
    4. Log.i("-Runnable->", i +"");
    5. i +=3;
    6. // The task to be done. Call this Runnable object again to implement the timer operation every two seconds.
    7. mHandler.postDelayed(runnable,300);
    8. Message msg = mHandler.obtainMessage();
    9. msg.arg1 +=i;
    10. MHandler. sendMessage (msg); // The handleMessage method that will be overwritten only when a message is sent
    11. // MHandler. sendMessageDelayed (Message. obtain (), 0); // The handleMessage method to be rewritten is triggered when a Message is sent.
    12. }
    13. };
     
  • Handler optimized
    1. privateHandler mHandler =newHandler(){
    2. @Override
    3. publicvoid handleMessage(Message msg){
    4. super.handleMessage(msg);
    5. //Log.i("-mHandler->", i + "");
    6. if(msg.arg1 >100){
    7. mHandler.removeCallbacks(runnable);
    8. }else{
    9. mProgressBar.setProgress(msg.arg1);
    10. }
    11. }
    12. };
     


  • From Weizhi note (Wiz)



    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.