Android--Long-term execution of timed loop tasks in the background

 
 
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.SystemClock;

import java.util.Timer;
import java.util.TimerTask;

public class MyService extends Service {

    private Runnable runnable;
    private Handler handler;
    private int Time = 1000*3;//cycle time
    private int anHour =8*60*60*1000;// This is the number of milliseconds in 8 hours. In order to consume less traffic and power, it is automatically updated once every 8 hours
    private Timer timer = new Timer ();
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        /**
         * Method 1: Use Handler's postDelayed(Runnable, long) method
         */
        handler = new Handler ();
        runnable = new Runnable() {

            @Override
            public void run() {
                // handler has its own method to implement timer
                System.out.println("33331");
                handler.postDelayed(this, 1000*3);//Execute every 3s

            }
        };
        handler.postDelayed(runnable, 1000*60);//How long to delay to start the timer

        /**
         * Method 2: Using the combination of timer and TimerTask
         */
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                System.out.println("99999998");
            }
        };
        timer.schedule(timerTask,
                1000, // delay 1 second execution
                Time);//cycle time


    }
    /**
     * Method 3: Using the AlarmManager mechanism
     */
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                System.out.println("99999988");//This is a scheduled task
            }
        }).start();
        AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
        long triggerAtTime = SystemClock.elapsedRealtime() + anHour;
        Intent intent2 = new Intent(this, AutoUpdateReceiver.class);
        PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent2, 0);
        manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pi);
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy ();

    }
}


public class AutoUpdateReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context, MyService.class);
        context.startService(i);
    }

}
Remember to register for broadcasts and services, and it is recommended to use method three.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325887298&siteId=291194637