如何停止和启动Asynctask。(Android)

时间:2016-06-21 09:40:10

标签: android android-asynctask

我有按钮Start和Stop,以及textview 我想点击按钮开始textview倒计时100-> 0。 当我点击按钮停止倒计时停止,然后我点击按钮再次开始倒计时继续。 对不起,我的英语不好。

1 个答案:

答案 0 :(得分:0)

那样的东西?

private Handler h = null;
final int delay = 2500; //millisecond
private Runnable myRunnable = new Runnable(){
    public void run(){
         runOnUiThread(new Runnable() {
            //Stuff here
         }
        }
    };

//To stop Handler when you leave your activity
@Override
public void onStop() {
    super.onStop();
    if (h != null && myRunnable != null) h.removeCallbacks(myRunnable);
    Log.d(TAG, "Stop Handler");
}

//Call this when you want to start your work
public void StartHandler(){
    h = new Handler();
    h.postDelayed(myRunnable, delay);
    Log.d(TAG, "Start Handler");
}