Android: CountDownTimer skips last onTick()!

前端 未结 12 699
眼角桃花
眼角桃花 2020-11-27 14:51

Code:

public class SMH extends Activity {  

    public void onCreate(Bundle b) {  
        super.onCreate(b);  
        setContentView(R.layou         


        
12条回答
  •  旧时难觅i
    2020-11-27 15:16

    I don't know why the last tick is not working but you can create your own timer with Runable , for example.

    class MyCountDownTimer {
        private long millisInFuture;
        private long countDownInterval;
        public MyCountDownTimer(long pMillisInFuture, long pCountDownInterval) {
                this.millisInFuture = pMillisInFuture;
                this.countDownInterval = pCountDownInterval;
            }
        public void Start() 
        {
            final Handler handler = new Handler();
            Log.v("status", "starting");
            final Runnable counter = new Runnable(){
    
                public void run(){
                    if(millisInFuture <= 0) {
                        Log.v("status", "done");
                    } else {
                        long sec = millisInFuture/1000;
                        Log.v("status", Long.toString(sec) + " seconds remain");
                        millisInFuture -= countDownInterval;
                        handler.postDelayed(this, countDownInterval);
                    }
                }
            };
    
            handler.postDelayed(counter, countDownInterval);
        }
    }
    

    and to start it,

    new MyCountDownTimer(10000, 2000).Start();
    

    EDIT FOR GOOFY'S QUESTION

    you should have a variable to hold counter status (boolean) . then you can write a Stop() method like Start().

    EDIT-2 FOR GOOFY'S QUESTION

    actually there is no bug on stopping counter but there is a bug on start again after stop(resume).

    I'm writing a new updated full code that I had just tried and it's working. It's a basic counter that show time on screen with start and stop button.

    class for counter

    public class MyCountDownTimer {
        private long millisInFuture;
        private long countDownInterval;
        private boolean status;
        public MyCountDownTimer(long pMillisInFuture, long pCountDownInterval) {
                this.millisInFuture = pMillisInFuture;
                this.countDownInterval = pCountDownInterval;
                status = false;
                Initialize();
        }
    
        public void Stop() {
            status = false;
        }
    
        public long getCurrentTime() {
            return millisInFuture;
        }
    
        public void Start() {
            status = true;
        }
        public void Initialize() 
        {
            final Handler handler = new Handler();
            Log.v("status", "starting");
            final Runnable counter = new Runnable(){
    
                public void run(){
                    long sec = millisInFuture/1000;
                    if(status) {
                        if(millisInFuture <= 0) {
                            Log.v("status", "done");
                        } else {
                            Log.v("status", Long.toString(sec) + " seconds remain");
                            millisInFuture -= countDownInterval;
                            handler.postDelayed(this, countDownInterval);
                        }
                    } else {
                        Log.v("status", Long.toString(sec) + " seconds remain and timer has stopped!");
                        handler.postDelayed(this, countDownInterval);
                    }
                }
            };
    
            handler.postDelayed(counter, countDownInterval);
        }
    }
    

    activity class

    public class CounterActivity extends Activity {
        /** Called when the activity is first created. */
        TextView timeText;
        Button startBut;
        Button stopBut;
        MyCountDownTimer mycounter;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            timeText = (TextView) findViewById(R.id.time);
            startBut = (Button) findViewById(R.id.start);
            stopBut = (Button) findViewById(R.id.stop);
            mycounter = new MyCountDownTimer(20000, 1000);
            RefreshTimer();
        }
    
        public void StartTimer(View v) {
            Log.v("startbutton", "saymaya basladi");
            mycounter.Start();
        }
    
        public void StopTimer(View v) {
            Log.v("stopbutton", "durdu");
            mycounter.Stop();
        }
    
        public void RefreshTimer() 
        {
            final Handler handler = new Handler();
            final Runnable counter = new Runnable(){
    
                public void run(){
                    timeText.setText(Long.toString(mycounter.getCurrentTime()));
                    handler.postDelayed(this, 100);
                }
            };
    
            handler.postDelayed(counter, 100);
        }
    }
    

    main.xml

    
    
        
        
        
        
    
    

提交回复
热议问题