Scheduling recurring task in Android

前端 未结 5 2210
南方客
南方客 2020-11-22 09:12

I\'m designing an app that has a recurring task of sending presence to a dedicated server as long as the app is in foreground.

In my searches across the web I saw a

5条回答
  •  -上瘾入骨i
    2020-11-22 09:32

    I have created on time task in which the task which user wants to repeat, add in the Custom TimeTask run() method. it is successfully reoccurring.

     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;
     import android.content.Intent;
    
     public class MainActivity extends Activity {
    
         CheckBox optSingleShot;
         Button btnStart, btnCancel;
         TextView textCounter;
    
         Timer timer;
         MyTimerTask myTimerTask;
    
         int tobeShown = 0  ;
    
    @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);
        tobeShown = 1;
    
        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, 1000);
        }
    
        btnStart.setOnClickListener(new OnClickListener(){
    
            @Override
            public void onClick(View arg0) {
    
    
                Intent i = new Intent(MainActivity.this, ActivityB.class);
                startActivity(i);
    
                /*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, 1000);
                }*/
            }});
    
        btnCancel.setOnClickListener(new OnClickListener(){
    
            @Override
            public void onClick(View v) {
                if (timer!=null){
                    timer.cancel();
                    timer = null;
                }
            }
        });
    
    }
    
    @Override
    protected void onResume() {
        super.onResume();
    
        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, 1000);
        }
    }
    
    
    @Override
    protected void onPause() {
        super.onPause();
    
        if (timer!=null){
            timer.cancel();
            timer = null;
        }
    
    }
    
    @Override
    protected void onStop() {
        super.onStop();
    
        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);
                }});
        }
    }
    

    }

提交回复
热议问题