The Method of Closing Android Handler postDelayed of and the Problems Encountered

  • 2021-12-12 05:54:55
  • OfStack

Preface

Recently, when using Handler, I found 1 wrong usage method

It's direct new like this


new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                showPaySuccessDialog.dismiss();
            }
        }, 1000);

This usage is also true for simple business situations, such as shutting down an dialog at regular intervals

But for complex business scenarios, it is wrong to use this way, isn't it wrong? If you use this way, there is bug

Problems encountered

I use direct new Handler() To execute a method of playing voice
The code is as follows


//todo  Detect here 
  new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (!MediaplayerUtil.isPlaying()) {
                        MediaplayerUtil.playMusic(context, R.raw.pay_tips);
                    }
                }
            }, 15 * 1000);

I thought it was right, but there was a problem in the test
The pages are closed, and this voice broadcast is still going on. Ha, ha, ha, my colleagues were shocked to hear it
It's embarrassing that the page is gone and the voice is still playing
At that time, when I heard this voice, I heard the problem in one ear

Because I thought something was wrong when I wrote it, there might be problems, so I added one TODO Mark
Prompt that there may be problems here

Solution

In the final analysis, my writing is not formal enough. Lazy behavior hahaha

1. Create an Handler object first


Handler handler=new Handler();

2. Then create an Runnable object


Runnable runnable=new Runnable(){
   @Override
   public void run() {
	    // TODO Auto-generated method stub
   	    // To do, call this again here Runnable Object to implement every two seconds 1 Time timer operation 
    handler.postDelayed(this, 2000);
   }
};

3. Use the PostDelayed method and call the Runnable object two seconds later

In fact, one timer of one 2s is realized


handler.postDelayed(runnable, 2000);

4. If you want to turn off this timer, you can do this


handler.removeCallbacks(runnable);

Correct use of Handler timer


// Initialization first 
 private Handler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        handler = new Handler();
 
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                //  This is a loop   Interval 3s Bomb 1 Times Toast
                handler.postDelayed(this, 3 *1000);
                Toast.makeText(this, " Delay 5s", Toast.LENGTH_SHORT).show();
            }
        };
        //  Delay 2s Bomb Toasat
        handler.postDelayed(runnable,2000);
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // At the end of the page   Empty queue message 
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
            handler = null;
        }
    }

Related articles: