在Android应用中暂停计时器

时间:2014-03-27 14:45:42

标签: android multithreading

在一个release方法中,将数据插入到另一个数据库的数据库中,执行时更新SQLite抛出异常,我需要暂停和恢复线程。我的计划是运行一个方法来查询我的数据库,以免造成错误。

我的计时器,

cron = new Timer();
    cron.schedule(scanTask = new TimerTask() {
         @Override
         public void run() {
             startService(new Intent(getBaseContext(), CronPush.class));
         }
    }, 0, 30000);



@Override
    protected Void doInBackground(Void... arg0) {
        cron.cancel();
        Sincronizacion s = new Sincronizacion();
        s.sincronizarBD(MenuEvento.this, codigoEvento);
        resume();
        return null;
    }


public void resume() {
    cron = new Timer();
    cron.schedule(scanTask = new TimerTask() {
         @Override
         public void run() {
             startService(new Intent(getBaseContext(), CronPush.class));
         }
    }, 0, 30000);
}


03-27 23:19:49.520: E/AndroidRuntime(4603): FATAL EXCEPTION: AsyncTask #3

03-27 23:19:49.520:E / AndroidRuntime(4603):java.lang.RuntimeException:执行doInBackground()时发生错误 03-27 23:19:49.520:E / AndroidRuntime(4603):在android.os.AsyncTask $ 3.done(AsyncTask.java:299) 03-27 23:19:49.520:E / AndroidRuntime(4603):at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) 03-27 23:19:49.520:E / AndroidRuntime(4603):at java.util.concurrent.FutureTask.setException(FutureTask.java:219) 03-27 23:19:49.520:E / AndroidRuntime(4603):at java.util.concurrent.FutureTask.run(FutureTask.java:239) 03-27 23:19:49.520:E / AndroidRuntime(4603):at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 03-27 23:19:49.520:E / AndroidRuntime(4603):at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:573) 03-27 23:19:49.520:E / AndroidRuntime(4603):at java.lang.Thread.run(Thread.java:856) 03-27 23:19:49.520:E / AndroidRuntime(4603):引起:java.lang.IllegalStateException:已经安排了TimerTask 03-27 23:19:49.520:E / AndroidRuntime(4603):at java.util.Timer.scheduleImpl(Timer.java:572) 03-27 23:19:49.520:E / AndroidRuntime(4603):at java.util.Timer.schedule(Timer.java:481) 03-27 23:19:49.520:E / AndroidRuntime(4603):at com.androidaz.scanner.MenuEvento.resume(MenuEvento.java:266) 03-27 23:19:49.520:E / AndroidRuntime(4603):at com.androidaz.scanner.MenuEvento $ sincronizarBD.doInBackground(MenuEvento.java:216) 03-27 23:19:49.520:E / AndroidRuntime(4603):at com.androidaz.scanner.MenuEvento $ sincronizarBD.doInBackground(MenuEvento.java:1) 03-27 23:19:49.520:E / AndroidRuntime(4603):在android.os.AsyncTask $ 2.call(AsyncTask.java:287) 03-27 23:19:49.520:E / AndroidRuntime(4603):at java.util.concurrent.FutureTask.run(FutureTask.java:234)

1 个答案:

答案 0 :(得分:0)

在创建新的Timer.cancel()对象再次安排后,如果抛出异常,请使用Timer删除它。使用Timer,您无法暂停/恢复它,只需永久取消。

TimerTask cronTimerTask = new TimerTask() {
     @Override
     public void run() {
         startService(new Intent(getBaseContext(), CronPush.class));
     }
};

Timer cron = new Timer();
cron.schedule(cronTimerTask, 0, 30000);

@Override
protected Void doInBackground(Void... arg0) {
    if (cron != null) {
        cron.cancel();
        cron.purge();
    }
    Sincronizacion s = new Sincronizacion();
    s.sincronizarBD(MenuEvento.this, codigoEvento);
    resume();
    return null;
}


public void resume() {
    try {
        cron = new Timer();
        cron.schedule(cronTimerTask, 0, 30000);
    } catch (Throwable e) {
        cron = null;
    }
}