This is the first day of my participation in the Challenge of Writing more in November.The final text challenge in 2021

background

Some tasks in a project are not executed immediately and need to be executed after a specified amount of time, possibly multiple iterations of the task. This time the role of timer is very useful, the following is a brief introduction to the common steps in Android timer implementation

Introduction to the

In Android, commonly used timers are mainly implemented as follows:

  • Use Handler’s postDelayed(Runnable, long) method
  • Use Handler in combination with timer and TimerTask
  • Use a combination of AlarmManager and PendingIntent

Handler for delay

Handler is used to process received messages. This is only the main method, of course there are other methods in Handler for implementation, if you are interested in the API, there is no more explanation. 1. Define a Handler class that handles incoming messages.

Handler handler = new Handler(){
    @Override
    public void handleMessage(@NonNull Message msg) {
        // The operation to be performed
        super.handleMessage(msg); }};Copy the code

Create an object that implements the Runnable interface.

Runnable runnable = new Runnable() {
    @Override
    public void run(a) {
        // Delay tasks that need to be done
        handler.postDelayed(runnable,3000); }};Copy the code

3. Start the timer

     handler.postDelayed(runnable,3000);
Copy the code

Handler+timer

1. Define the timer and Handler handle.

private final Timer timer = new Timer();
    private TimerTask task;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
        // The operation to be performed
            super.handleMessage(msg); }};Copy the code

2. Initialize the timer task

 task = new TimerTask() { 
        @Override 
        public void run(a) { 
            Message message = new Message(); 
            message.what = 1; handler.sendMessage(message); }};Copy the code

3. Start the timer

    timer.schedule(task, 3000.3000);
Copy the code

AlarmManager+PendingIntent

1. First get the AlarmManager object from the context

alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Copy the code

2, Obtain the PendingIntent object

  • structureIntentobject
Intent intent = new Intent(context, RestLoginBroadcastReceiver.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setAction("restLogin");
Copy the code
  • throughgetBroadcastAccess to thePendingIntentobject
pi = PendingIntent.getBroadcast(context, 0, intent,
        PendingIntent.FLAG_UPDATE_CURRENT);
Copy the code

3. Send a PendingIntent object that delays the intent through the AlarmManager object

alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 5 * 60 * 1000, pi);
Copy the code

4. Cancel the alarm

public static void cancel(a) {
    if(alarmManager ! =null) {
        if(pi ! =null) { alarmManager.cancel(pi); }}}Copy the code