Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize timing Loop background tasks in Android AlarmManager

Shulou Source: shulou.com Published: 2022-06-02 10:12:23 09月30日 Update

This article will explain in detail how to achieve timing loop background tasks in Android AlarmManager. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Introduction to AlarmManager

AlarmManager is a system-level prompt service commonly used in Android, which broadcasts a specified Intent for us at a specific time. To put it simply, we set a time, and then when that time comes, AlarmManager broadcasts an Intent for us, usually we use PendingIntent.

Project function introduction:

AlarmService simulates background tasks, regularly initiates broadcast AlarmReceive and starts AlarmService, achieving the effect of starting Service in a cycle.

Ensure that background tasks are not killed by the system through the endless loop of Service and Receiver.

1.AlarmService class

/ * A scheduled task * / public class AlarmService extends Service {/ * update data every 1 minute * / private static final int ONE_Miniute=60*1000; private static final int PENDING_REQUEST=0 When public AlarmService () {} / * calls Service, the method * / @ Override public int onStartCommand (Intent intent, int flags, int startId) {/ / simulates the background operation new Thread (new Runnable () {@ Override public void run () {Log.e ("wj", "loop executed, ." + System.currentTimeMillis ());}}). Start () / / start broadcasting AlarmManager alarmManager= (AlarmManager) getSystemService (ALARM_SERVICE) regularly via AlarmManager; long triggerAtTime=SystemClock.elapsedRealtime () + ONE_Miniute;// the millisecond book from startup to the present (mobile phone sleep (sleep) also includes Intent i=new Intent (this, AlarmReceive.class); PendingIntent pIntent=PendingIntent.getBroadcast (this,PENDING_REQUEST,i,PENDING_REQUEST); alarmManager.set (AlarmManager.ELAPSED_REALTIME_WAKEUP,triggerAtTime,pIntent); return super.onStartCommand (intent, flags, startId) } @ Override public IBinder onBind (Intent intent) {/ / TODO: Return the communication channel to the service. Throw new UnsupportedOperationException ("Not yet implemented");}}

2 AlarmReceive class

Public class AlarmReceive extends BroadcastReceiver {@ Override public void onReceive (Context context, Intent intent) {/ / Loop launch Service Intent i = new Intent (context, AlarmService.class); context.startService (I);}}

3 start Service

Public void startService (View view) {Intent intent=new Intent (this, AlarmService.class); startService (intent);}

Don't forget to register broadcasts and services with AndroidMainfest:

On how to achieve Android AlarmManager timing cycle background tasks to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Loops tasks background broadcast time content articles more knowledge profiles articles systems services yes functions that is commonly used mobile phones effects data Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux macOS Microsoft Shulou Information Shulou Tech Info