如何取消嵌套计时器?

时间:2018-05-09 03:30:53

标签: java android

我有两个计时器:

  • theFirstCountDownTimer;
  • theGraceCountDownTimer;

我有一个使用这两个计时器的服务,其中theGraceCountDownTimer嵌套在FirstCountdownTimer中。在OnDestroy方法中,我想取消两个计时器。

private CountDownTimer theFirstCountDownTimer;
private CountDownTimer theGraceCountDownTimer;

//////////////////////////////////////

@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.

    return null;
}

//////////////////////////////////////

@Override
public int onStartCommand(Intent intent, int flags, int startId){

    Log.i("Running", "The Service");

    final SharedPreferences myPref = this.getSharedPreferences("testPref321", Context.MODE_PRIVATE);

    theFirstCountDownTimer = new CountDownTimer(myPref.getInt("seekBar", 10000) * 1000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            String myMilis = Long.toString(millisUntilFinished);
            Log.i("Timer One", "OnTick-" + myMilis);
        }

        @Override
        public void onFinish() {

            Log.i("Timer One", "OnFinish");

            theGraceCountDownTimer = new CountDownTimer(10000, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {

                    String myMilis = Long.toString(millisUntilFinished);
                    Log.i("Second Timer!", myMilis);

                }

                @Override
                public void onFinish() {

                    Log.i("Finished!", "Heck Ya!");

                }
            }.start();

        }
    }.start();


    return START_STICKY;
}

//////////////////////////////////////

@Override
public  void onDestroy(){

    theGraceCountDownTimer.cancel();
    //This is the error

    theFirstCountDownTimer.cancel();

    super.onDestroy();
}

此外,我知道该错误特别与该行,theGraceCountDownTimer.cancel。

错误说明:

Process: com.example.jackson.dmtapp, PID: 22854
java.lang.RuntimeException: Unable to stop service com.example.jackson.dmtapp.TheService@8090f39: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.os.CountDownTimer.cancel()' on a null object reference
    at android.app.ActivityThread.handleStopService(ActivityThread.java:3479)
    at android.app.ActivityThread.-wrap27(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1638)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6316)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.os.CountDownTimer.cancel()' on a null object reference
    at com.example.jackson.dmtapp.TheService.onDestroy(TheService.java:93)
    at android.app.ActivityThread.handleStopService(ActivityThread.java:3462)
    at android.app.ActivityThread.-wrap27(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1638) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6316) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762) 

1 个答案:

答案 0 :(得分:0)

您需要在两个计时器上调用cancel()。由于您在theFirstCountDownTimer内初始化onStartCommand(),因此它不能为空。但是theGraceCountDownTimertheFirstCountDownTimer完成后初始化时可以为@Override public void onDestroy(){ if(theGraceCountDownTimer!=null) theGraceCountDownTimer.cancel(); theFirstCountDownTimer.cancel(); super.onDestroy(); }

while true
do
    echo "loren"
    # execution desired command in this block.
    sleep $scheduled_Interval

done