简体   繁体   English

按下按钮时如何在Java中销毁CountdownTimer?

[英]How To Destroy CoundownTimer in Java when the button is pressed?

I make a timer with a count time of 5 seconds, then when I press the exit button the counter automatically stops?我制作了一个计时 5 秒的计时器,然后当我按下退出按钮时,计数器会自动停止?

Here is my timer code:这是我的计时器代码:

    public void startTimer(final long finish, long tick) {
        CountDownTimer t;
        t = new CountDownTimer(finish, tick) {

            public void onTick(long millisUntilFinished) {
                long remainedSecs = millisUntilFinished / 1000;
                textTimer.setText("" + (remainedSecs / 60) + ":" + (remainedSecs % 60));// manage it accordign to you
            }

            public void onFinish() {
                textTimer.setText("00:00");
                Toast.makeText(FloatingVideoWidgetShowService.this, "Waktu Habis", Toast.LENGTH_SHORT).show();
                long seek = videoView.getCurrentPosition();
                videoView.setKeepScreenOn(false);
                stopSelf();
                WritableMap args = new Arguments().createMap();
                args.putInt("index", index);
                args.putInt("seek", (int) seek);
                args.putString("url", playingVideo.getString("url"));
                args.putString("type", "close");

                sendEvent(reactContext, "onClose", args);
                onDestroy();
                cancel();
            }
        }.start();

    }

And this is my code when pressing the stop / exit button :这是我按下停止/退出按钮时的代码:

        floatingWindow.findViewById(R.id.btn_deny).setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View view) {
                long seek = videoView.getCurrentPosition();
                videoView.setKeepScreenOn(false);
                stopSelf();
                WritableMap args = new Arguments().createMap();
                args.putInt("index", index);
                args.putInt("seek", (int) seek);
                args.putString("url", playingVideo.getString("url"));
                args.putString("type", "close");

                sendEvent(reactContext, "onClose", args);
                onDestroy();
            }
        });

How so when btn_deny is clicked Cuntdowntimer stops and does not force close?当 btn_deny 被点击时,Cuntdowntimer 停止并且不强制关闭怎么办?

Thanks.谢谢。

You can't use onDestroy() to close your activity or fragment.您不能使用onDestroy()关闭您的活动或片段。 Instead, you need to call finish() .相反,您需要调用finish()

To close the CountDownTimer , you need to make it a class scope variable.要关闭CountDownTimer ,您需要将其设为类范围变量。 Prepare the timer at your startTimer then stop the timer by calling t.cancel() like the following code:startTimer准备计时器,然后通过调用t.cancel()停止计时器,如下面的代码:

public class YourActivity extends Activity {
   // Declare the variable to be accessed later.
   CountDownTimer t;

   ...

   public void startTimer(final long finish, long tick) {
     t = new CountDownTimer(finish, tick) {
         ...
     }.start();

   }


   private void yourOtherMethod() {

    floatingWindow.findViewById(R.id.btn_deny).setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {
          if(t != null) t.cancel();
          ...
       }
    });
   }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM