How can I pause the timer in android?

后端 未结 2 616
天涯浪人
天涯浪人 2021-01-07 05:04

I have gone through the link http://dewful.com/?tag=basic-android-timer regarding the timer application in android. It is working fine. I need to add the pause butto

相关标签:
2条回答
  • 2021-01-07 05:42
    public class TimerActivity extends Activity
    {
        EditText e1;
        MyCount counter;
        Long s1;
        /** Called when the activity is first created. */ 
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            e1=(EditText)findViewById(R.id.editText1);
            counter= new MyCount(5000,1000);
            counter.start();
        }
        public void asdf(View v)
        {
            switch(v.getId())
            {
                case R.id.button1:         counter.cancel();
                break;
                case R.id.button2:         counter= new MyCount(s1,1000);
                counter.start();
            }
        }
        public class MyCount extends CountDownTimer
        {
            public MyCount(long millisInFuture, long countDownInterval)
            {
                super(millisInFuture, countDownInterval);
            }
            @Override     public void onFinish()
            {
                e1.setText("DONE");
            }
            @Override     public void onTick(long millisUntilFinished)
            {
                s1=millisUntilFinished;
                e1.setText("left:"+millisUntilFinished/1000);
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-07 05:50
    public void time(long m){
        timer=new CountDownTimer(m, 1000) {
    
                public void onTick(long millisUntilFinished) {
    
                    tv_timer.setText(formatTime(millisUntilFinished));
    
                }
    
                public void onFinish() {
                    tv_timer.setText("done!");
                }
    
             }.start();
    
     }
    

    i have used this for timer and i have put button for pause and use this

        case R.id.bT_PAUSE:
        String s_time = null;
    
        try{
        if(Bt_pause.getText().equals("PAUSE")){
    
        s_time=tv_timer.getText().toString();
        timer.cancel();
        String[] Pause_time=s_time.split(":");
        m=Long.parseLong(Pause_time[0].trim());
        n=Long.parseLong(Pause_time[1].trim());
        m=(m*60)+n;
        m=m*1000;
        Bt_pause.setText("RESUME");
    
        }else if(Bt_pause.getText().equals("RESUME")){
            //min_longmillis=Long.parseLong(sss);
            //min_longmillis=min_longmillis*1000*60;
            //min_longmillis=m;
            //timer.start();
            Toast.makeText(this,String.valueOf(m),Toast.LENGTH_SHORT).show();
            time(m);
            Bt_pause.setText("PAUSE");
        }
        }catch(Exception e){
            Toast.makeText(this,e.toString(),Toast.LENGTH_SHORT).show();
        }
        break;
    

    and this for format time

    public String formatTime(long millis) {  
        String output = "00:00";  
        long seconds = millis / 1000;  
        long minutes = seconds / 60;  
    
        seconds = seconds % 60;  
        minutes = minutes % 60;  
    
        String sec = String.valueOf(seconds);  
        String min = String.valueOf(minutes);  
    
        if (seconds < 10)  
            sec = "0" + seconds;  
        if (minutes < 10)  
            min= "0" + minutes;  
    
        output = min + " : " + sec;  
        return output;
    }//formatTime
    
    0 讨论(0)
提交回复
热议问题