android如何停止mHandler.postDelayed

时间:2012-04-06 12:22:16

标签: android

我有一个应用程序,我使用mHandler作为计时器....它工作正常,然后Runnable MuPdeteTimeTask工作....这是一个代码......

    mHandlerTime.removeCallbacks(mUpdateTimeOutTask);
                mHandlerTime.postDelayed(mUpdateTimeOutTask, delay_timeoutzz);   

....................................

    private Runnable mUpdateTimeOutTask = new Runnable() {
           public void run() {
               Toast.makeText(getApplicationContext(), "TimeOut of session is reached! Session will be force closed!", Toast.LENGTH_LONG).show();
               new UploadTask().onCancelled();
                       }
        };

我怎么能阻止它或想要定义“延迟”的新含义????

1 个答案:

答案 0 :(得分:1)

你肯定可以这样做,

拿两个变量,

boolean delayedAgain = false;
long newDelayedTimeFromNow=5000;

然后在你的runnable应用逻辑中,

Runnable  mUpdateTimeOutTask = new Runnable() {             
            @Override
            public void run() {
                if(delayedAgain)
                {
                    delayedAgain = false;
                    mHandlerTime.postDelayed(this, newDelayedTimeFromNow);  
                    return;
                }
                else
                {
                    // DO JOB ON TIME.
                }                   
            }
        };

现在从你的另一项任务开始,

if(somethingIsDone)
{
  delayedAgain = true;
  newDelayedTimeFromNow = 3000;
}