如果用户在2分钟内没有与应用程序交互,我想关闭我的应用程序,所以ihad实现了以下计时器但是timer.cancel()无法正常工作.it dosent stop.so应用程序崩溃
private static Timer mTimer1;
private static TimerTask mTt1;
private static Timer mTimer2;
private static TimerTask mTt2;
private static ApplicationPreferences mPrefernces = null;
public static void stopTimer() {
Log.v("stoptimer", "stoptimer");
if (mTimer1 != null) {
mTimer1.cancel();
mTimer1.purge();
mTt1.cancel();
}
}
public static void startTimer(final Activity context) {
try {
Log.v("startTimer", "startTimer");
stopTimer();
mTimer1 = new Timer();
mTt1 = new TimerTask() {
public void run() {
context.runOnUiThread(new Runnable() {
@Override
public void run() {
// show timeout popup when there no any interaction to user
showTimeoutPopup(context);
}
});
}
};
mTimer1.schedule(mTt1, 120000, 10000);
} catch (Exception e) {
Log.v("exception in starttimer", e.toString());
}
}