Android,暂停和恢复处理程序回调

时间:2010-12-13 12:34:28

标签: android callback handler

我有一个我正在使用的处理程序,如下所示:

handler.postDelayed(Play, 1000);

当我的应用程序onPause()在完成之前被调用时,我需要暂停它并告诉它不要执行“postDelayed”直到我恢复。

这是可能的,还是有另一种方式?

我的问题是,当调用onPause()时,我暂停音频(SoundManager),但如果此后调用此handler.postDelayed,音频将不会暂停,并将继续在后台播放我的应用程序。

@Override
public void onPause()
{
  Soundmanager.autoPause()
}

但是1000ms后的postDelayed再次开始播放音频。

5 个答案:

答案 0 :(得分:18)

您需要子类化Handler并实现如下的暂停/恢复方法(然后在您要暂停消息处理时调用handler.pause(),并在需要重新启动时调用handler.resume() ):

class MyHandler extends Handler {
    Stack<Message> s = new Stack<Message>();
    boolean is_paused = false;

    public synchronized void pause() {
        is_paused = true;
    }

    public synchronized void resume() {
        is_paused = false;
        while (!s.empty()) {
            sendMessageAtFrontOfQueue(s.pop());
        }
    }

    @Override
    public void handleMessage(Message msg) {
        if (is_paused) {
            s.push(Message.obtain(msg));
            return;
        }else{
               super.handleMessage(msg);
               // otherwise handle message as normal
               // ...
        }
    }
    //...
}

答案 1 :(得分:13)

您是否尝试过:

@Override
public void onPause()
{
  handler.removeCallbacks(Play);
  Soundmanager.autoPause()
}

GER

答案 2 :(得分:0)

public class YourActivity extends AppCompatActivity {

    private static boolean handlerflag=false;
    private Handler handler;
    private Runnable runnable;
    private int myind=0,index=0,count=0;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_activtiy);         
        //oncreate exe only
        handlerflag=true;
        handler = new Handler();
        startyourtime(0);
 }
  private void  startyourtime(int a) {

    myind=0;
    for (index=a; index<10 ;index++) {
            myind++;
            runnable=new Runnable() {
                count++;
                @Override
                public void run() {
                          //your code here
               }
            };handler.postDelayed(runnable, Constants.TIME_LIMIT * myind);

   }
    @Override
    protected void onPause() {
        super.onPause();
        handlerflag=false;
        handler.removeCallbacksAndMessages(null);
    }
    @Override
    protected void onResume() {
        super.onResume();
        if(!handlerflag)
        {
           startyourtime(count);

        }
    }
}

答案 3 :(得分:0)

修改CpcCrunch给出的答案。 handleMessage 对我不起作用,因此请使用 dispatchMessage 代替它。注意:以下代码是用Kotlin编写的:

class CustomHandler: Handler() {

    var s = Stack<Message>()
    var is_paused = false

    @Synchronized
    fun pause() {
        is_paused = true
    }

    @Synchronized
    fun resume() {
        is_paused = false
        while (!s.empty()) {
            sendMessageAtFrontOfQueue(s.pop())
        }
    }

    override fun dispatchMessage(msg: Message?) {
        if (is_paused) {
            s.push(Message.obtain(msg))
            return
        } else {
            super.dispatchMessage(msg)
        }
    }
}

答案 4 :(得分:0)

当想在队列中暂停/恢复Runnables时,我想出了CpnCrunch的替代方法。要使仍处于连接状态且处于脱机状态的方法被调用,请使其联机后恢复队列并执行所有可运行对象。

使用Handler代替使用ExecutorService

public class ExecutorQueueService extends ThreadPoolExecutor {
    private Stack<Runnable> runnables = new Stack<>();
    private boolean paused = false;

    public ExecutorQueueService() {
        super(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
    }

    public synchronized void pause() {
        paused = true;
    }

    public synchronized void resume() {
        paused = false;
        while (!runnables.empty()) {
            execute(runnables.pop());
        }
    }

    public synchronized boolean isPaused() {
        return paused;
    }

    @Override
    public void execute(Runnable runnable) {
        if (paused) {
            runnables.push(runnable);
        } else {
            super.execute(runnable);
        }
    }
}

使用它类似于Handler,但使用post(runnable)代替execute(runnable)