服务中的Android计时器 - 如何停止和启动

时间:2017-08-28 18:48:28

标签: android service timer

我看过很多帖子,但找不到我特定问题的答案。

以下服务运作良好。我可以开始了。开始。我可以阻止它。它停止了。我可以用新的间隔重新启动它。这也有效。当我停止它时,它不会停止。在onDestroy中,这是timer = null的情况。如果我在timer = null的情况下在onDestroy中插入timer.cancel,那么我会因为对空指针的操作而崩溃。然而,它仍然按日志输出所示运行!

如果有用的话,这是日志输出(在最后一个" noOutRows&#34之后;它只是继续运行)

E/ timer:  startservice
E/ timer:  timeInterval 1000
E/ timer:  isNotNull
E/ timer:  no noOutRows 1503945704743
E/ timer:  no noOutRows 1503945705712
E/ timer:  no noOutRows 1503945706714
E/ timer:  no noOutRows 1503945707713
E/ timer:  no noOutRows 1503945708712
E/ timer:  stopservice
E/ timer:  stop - isNotNull
E/ timer:  startservice
E/ timer:  timeInterval 5000
E/ timer:  isNull
E/ timer:  no noOutRows 1503945744406
E/ timer:  no noOutRows 1503945749407
E/ timer:  no noOutRows 1503945754407
E/ timer:  stopservice
E/ timer:  stop - isNull
E/ timer:  no noOutRows 1503945759407
E/ timer:  no noOutRows 1503945764407
E/ timer:  no noOutRows 1503945769773
  

这是我的代码

    public class LocalService extends Service
{
    public Integer timeInterval = 1000;
    private static Timer timer = new Timer();
    public IBinder onBind(Intent arg0)
    {
        return null;
    }

    @Override
    public void onCreate()
    {
        super.onCreate();
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(" timer"," startservice");
        int newTimeInterval = intent.getIntExtra("time_interval", -99);
            timeInterval = newTimeInterval;
            Log.e(" timer ", " timeInterval " + timeInterval);
            if (timer == null){
                Log.e(" timer"," isNull");
                Timer timer = new Timer();
                timer.scheduleAtFixedRate(new mainTask(), 0, timeInterval);
            } else {
                Log.e(" timer"," isNotNull");
                timer.scheduleAtFixedRate(new mainTask(), 0, timeInterval);
            }
        return START_STICKY;
    }
    private class mainTask extends TimerTask
    {
        public void run()
        {
            logHandler.sendEmptyMessage(0);
        }
    }
    public void onDestroy()
    {
        Log.e(" timer"," stopservice");
        if (timer != null) {
            Log.e(" timer"," stop - isNotNull");
            timer.cancel();
            timer = null;
        } else {
            Log.e(" timer"," stop - isNull");
        }
        super.onDestroy();
    }
    private final Handler logHandler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            int maxRows = MainActivity.Cache.alls.size()-1;
            if (maxRows > 0) {
                Log.e(" timer ", " outRows " + maxRows + " " + MainActivity.Cache.alls.get(maxRows).toString());
            } else {
                Log.e(" timer ", " no noOutRows " + MainActivity.utcNow().getTime());
            }
        }
    };
}

1 个答案:

答案 0 :(得分:0)

出于某种原因,我不记得每个人都同意不在Android中使用Timers,他们选择了Runnable / Handler组合。它就像这样

private static final int delay = 1000;
private final Handler handler = new Handler();

Runnable runnable = new Runnable() {
    @Override
    public void run() {
        yourAdvancedScheduledLogic();//pre or post as you wish
        if(!yourExitCondition()) {
            handler.postDelayed(this, delay);
        }
    }
};

runnable.run();//stars the timer