开发者

how can i stop a function in a service?

开发者 https://www.devze.com 2023-04-05 20:25 出处:网络
I have a function:: public void doSleep() { handler.postDelayed( new Runnable() { 开发者_高级运维public void run() { doCom(); }

I have a function::

public void doSleep() {
    handler.postDelayed(
        new Runnable() { 
      开发者_高级运维      public void run() { doCom(); } 
        }, 10000); 
}  

This is linked with doCom which starts this again. how can I finsh this at the end of the service?


you can also use

handler.removeCallbacks(Runnable r);


create a boolean flag in your class called sleep = true;

public void doSleep() {
    handler.postDelayed(
        new Runnable() { 
            public void run() { if(sleep) { doCom(); } } 
        }, 10000); 
}  

and set sleep = false; if you want to stop it.


use StopService() or StopSelf() methods where you want to stop

0

精彩评论

暂无评论...
验证码 换一张
取 消