我该如何停止从不同的活性(处理器和postAtTime)做了一个循环,创建它的人吗? [英] how do I stop a loop made ​​with (Handler and postAtTime) from a different activity, the one that created it?

查看:146
本文介绍了我该如何停止从不同的活性(处理器和postAtTime)做了一个循环,创建它的人吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题。我只好让我一个连续的循环与postAtTime的方法使用(处理器),但这种方法被放置在一个活动每x秒A,进入活动B当,然后回归A为pressing停止按钮finsih循环,我不能停止mHandler.removeCallbacks(..)我附上这里介绍的方法

按钮启动:

  mHandler.postAtTime(mUpdateTimeTask,100);

循环:

 私人的Runnable mUpdateTimeTask =新的Runnable(){
     公共无效的run(){
          Toast.makeText(getBaseContext(),OK!,Toast.LENGTH_SHORT).show();
          mHandler.postDelayed(这一点,1000);
      }
   };

按钮停止:

  mHandler.removeCallbacks(mUpdateTimeTask);


解决方案

  //在你的处理器
共享preferences SP = context.get preferences();
布尔环= sp.getBoolean(should_loop_ad_infinatium,FALSE);
如果(循环){
    Toast.makeText(上下文,确定,Toast.LENGTH_SHORT).show();
    mHandler.postDelayed(这一点,1000);
}

从另一个活动

 共享preferences SP = context.get preferences();
编辑E = sp.edit();
e.putBoolean(should_loop_ad_infinatium,FALSE);
e.commit();

I have a problem. I had a method that makes me a continuous loop with postAtTime every x seconds using (Handler), except that this method is placed in an activity A, when going into the activity B and then return to A for pressing stop button for finsih loop, i can not stop it with mHandler.removeCallbacks (..) I am attaching here the method

Button Start :

mHandler.postAtTime(mUpdateTimeTask, 100);

Loop :

private  Runnable mUpdateTimeTask = new Runnable() {
     public void run() {
          Toast.makeText(getBaseContext(),"ok!",Toast.LENGTH_SHORT).show();
          mHandler.postDelayed(this, 1000);
      }
   };

Button Stop :

mHandler.removeCallbacks(mUpdateTimeTask);

解决方案

// In your handler
SharedPreferences sp = context.getPreferences();
boolean loop = sp.getBoolean("should_loop_ad_infinatium", false);
if (loop) {
    Toast.makeText(context, "ok", Toast.LENGTH_SHORT).show();
    mHandler.postDelayed(this, 1000);
}

from another activity

SharedPreferences sp = context.getPreferences();
Editor e = sp.edit();
e.putBoolean("should_loop_ad_infinatium", false);
e.commit();

这篇关于我该如何停止从不同的活性(处理器和postAtTime)做了一个循环,创建它的人吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆