暂停/恢复处理程序发布延迟

时间:2014-03-01 04:28:20

标签: android handler onresume onpause

我试图在延迟2秒后开始活动。代码 -

runnable = new Runnable() {
        @Override
        public void run() {
            final Intent i = new Intent(PreviewCoordinatesActivity.this,
                    StylePreviewActivity.class);
            i.putExtra(AppConstants.IMAGEPATH, sImagePath);
            moveNextToAndFinish(i);
        }
    };
    handler.postDelayed(runnable, DELAY);

我希望在活动暂停时暂停操作,并在活动恢复到前台后恢复操作。

目前我只是在暂停活动时删除回调 -

@Override
protected void onPause() {
    super.onPause();
    if (handler != null) {
        if (runnable != null)
            handler.removeCallbacks(runnable);
    }

}

1 个答案:

答案 0 :(得分:1)

以下是我使用的实现:

private static final long PAUSED_TIME_UNSET = 0L;

private Handler delayHandler;
private long pausedTime;
private List<Message> pendingMessages;
private Map<Message, Long> messageTimeMap;

//this should not be called while paused
protected Runnable runAfterDelay(Runnable toRun, long delayMillis) {
    if (delayHandler == null) {
        delayHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                pendingMessages.remove(msg);
                if (msg.obj instanceof Runnable) {
                    ((Runnable) msg.obj).run();
                }
                return true;
            }
        });
    }
    Message m = Message.obtain();
    m.obj = toRun;
    pendingMessages.add(m);
    delayHandler.sendMessageDelayed(m, delayMillis);
    return toRun;
}

protected void cancelDelayedRunnable(Runnable runnable) {
    for (Iterator<Message> iter = pendingMessages.iterator(); iter.hasNext(); ) {
        Message msg = iter.next();
        if (msg.obj == runnable) {
            iter.remove();
            break; //assume all runnables are unique
        }
    }
    delayHandler.removeCallbacksAndMessages(runnable);
}

protected void cancelAllDelayedRunnables() {
    if (delayHandler != null) {
        delayHandler.removeCallbacksAndMessages(null);
        delayHandler = null;
    }
    pendingMessages.clear();
}

public void pause() {
    if (delayHandler != null && pausedTime == PAUSED_TIME_UNSET) {
        pausedTime = SystemClock.uptimeMillis();

        //need to copy the Messages because their data will get cleared in removeCallbacksAndMessages
        List<Message> copiedMessages = new ArrayList<Message>();
        for (Message msg : pendingMessages) {
            Message copy = Message.obtain();
            copy.obj = msg.obj;
            messageTimeMap.put(copy, msg.getWhen()); //remember the time since unable to set directly on Message
            copiedMessages.add(copy);
        }
        //remove all messages from the handler
        delayHandler.removeCallbacksAndMessages(null);
        pendingMessages.clear();

        pendingMessages.addAll(copiedMessages);
    }
}

public void resume() {
    if (delayHandler != null && pausedTime != PAUSED_TIME_UNSET) {
        for (Message msg : pendingMessages) {
            long msgWhen = messageTimeMap.get(msg);
            long timeLeftForMessage = msgWhen - pausedTime;
            delayHandler.sendMessageDelayed(msg, timeLeftForMessage);
        }
        messageTimeMap.clear();
        pausedTime = PAUSED_TIME_UNSET;
    }
}