Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize timing Loop background tasks in Android AlarmManager

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report