Android equivalent of setTimeout and clearTimeout of javascript?

你离开我真会死。 提交于 2019-12-12 13:12:20

问题


There's an answer for setTimeout https://stackoverflow.com/a/18381353/433570

It doesn't provide info whether we can cancel the timer as we could in javascript.

Is there a timer that is cancellable in android?


回答1:


According to the documentation of Handler

public final void removeCallbacks (Runnable r)

Added in API level 1

Remove any pending posts of Runnable r that are in the message queue.

Example code:

Runnable runnable = new Runnable() {
    public void run() {
        Log.i("Tag", "Runnable running!");
    }
};

Handler handler = new android.os.Handler();
handler.postDelayed(runnable, 1000);

handler.removeCallbacks(runnable);



回答2:


For instance, you may use removeMessages, removeCallbacks methods



来源:https://stackoverflow.com/questions/33689125/android-equivalent-of-settimeout-and-cleartimeout-of-javascript

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!