Repeat a task with a time delay?

后端 未结 12 1409
小蘑菇
小蘑菇 2020-11-22 02:14

I have a variable in my code say it is \"status\".

I want to display some text in the application depending on this variable value. This has to be done with a speci

12条回答
  •  借酒劲吻你
    2020-11-22 02:53

    Timer is another way to do your work but be quiet sure to add runOnUiThread if you are working with UI.

        import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Timer;
    import java.util.TimerTask;
    
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.TextView;
    import android.app.Activity;
    
    public class MainActivity extends Activity {
    
     CheckBox optSingleShot;
     Button btnStart, btnCancel;
     TextView textCounter;
    
     Timer timer;
     MyTimerTask myTimerTask;
    
     @Override
     protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      optSingleShot = (CheckBox)findViewById(R.id.singleshot);
      btnStart = (Button)findViewById(R.id.start);
      btnCancel = (Button)findViewById(R.id.cancel);
      textCounter = (TextView)findViewById(R.id.counter);
    
      btnStart.setOnClickListener(new OnClickListener(){
    
       @Override
       public void onClick(View arg0) {
    
        if(timer != null){
         timer.cancel();
        }
    
        //re-schedule timer here
        //otherwise, IllegalStateException of
        //"TimerTask is scheduled already" 
        //will be thrown
        timer = new Timer();
        myTimerTask = new MyTimerTask();
    
        if(optSingleShot.isChecked()){
         //singleshot delay 1000 ms
         timer.schedule(myTimerTask, 1000);
        }else{
         //delay 1000ms, repeat in 5000ms
         timer.schedule(myTimerTask, 1000, 5000);
        }
       }});
    
      btnCancel.setOnClickListener(new OnClickListener(){
    
       @Override
       public void onClick(View v) {
        if (timer!=null){
         timer.cancel();
         timer = null;
        }
       }
      });
    
     }
    
     class MyTimerTask extends TimerTask {
    
      @Override
      public void run() {
       Calendar calendar = Calendar.getInstance();
       SimpleDateFormat simpleDateFormat = 
         new SimpleDateFormat("dd:MMMM:yyyy HH:mm:ss a");
       final String strDate = simpleDateFormat.format(calendar.getTime());
    
       runOnUiThread(new Runnable(){
    
        @Override
        public void run() {
         textCounter.setText(strDate);
        }});
      }
    
     }
    
    }
    

    and xml is...

    
    
    
    
    

    Another Way to use CountDownTimer

    new CountDownTimer(30000, 1000) {
    
         public void onTick(long millisUntilFinished) {
             mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
         }
    
         public void onFinish() {
             mTextField.setText("done!");
         }
      }.start();
    

    Schedule a countdown until a time in the future, with regular notifications on intervals along the way. Example of showing a 30 second countdown in a text field:

    For Details

提交回复
热议问题