Stop handler.postDelayed()

前端 未结 4 1410
被撕碎了的回忆
被撕碎了的回忆 2020-11-29 21:00

I call multiple Handlers by new Handler().postDelayed(new Runnable()..... How can I stop it when I click on back?

public class MyActivity extends AppCompatA         


        
4条回答
  •  一整个雨季
    2020-11-29 21:26

      Boolean condition=false;  //Instance variable declaration.
    
     //-----------------Inside oncreate--------------------------------------------------- 
      start =(Button)findViewById(R.id.id_start);
            start.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    starthandler();
    
                    if(condition=true)
                    {
                        condition=false;
                    }
    
    
                }
            });
    
            stop=(Button) findViewById(R.id.id_stoplocatingsmartplug);
    
            stop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    stophandler();
    
                }
            });
    
    
        }
    
    //-----------------Inside oncreate---------------------------------------------------
    
     public void starthandler()
        {
    
            handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
    
    
                    if(!condition)
                    {
                        //Do something after 100ms 
    
    
                    }
    
                }
            }, 5000);
    
        }
    
    
        public void stophandler()
        {
            condition=true;
        }
    

提交回复
热议问题