恢复Runnable()使应用程序出错

时间:2018-10-22 06:46:09

标签: android handler runnable

我有一个Handler(),需要在设备锁定时暂停。我使用了代码removeCallBacks(runnable),但是在恢复后,我再次调用handler.post(runnable),但是应用程序崩溃了。我该怎么做?。这是代码

 //globally
 Handler handler = new Handler();

private final Runnable mRunnable = new Runnable() {
    @Override
    public void run() {
        // Open RF Card interface
        validateCardValue();

        handler.postDelayed(mRunnable, 2000);
    }
};



//onPause()
   @Override
public void onPause() {
    handler.removeCallbacks(mRunnable);
    super.onPause();
}

//onResume()
 @Override
public void onResume() {
    Log.e("resume!","resume");
    try{
        handler.post(mRunnable);
    }catch (Throwable e){
        e.printStackTrace();
    }
    super.onResume();
}

0 个答案:

没有答案