In depth analysis of alarm manager usage of Android alarm service

This article describes the use of Android alarm service AlarmManager. Share with you for your reference, as follows:

There is an alarmmanagerservie service program corresponding to alarmmanage, which really provides alarm service. It mainly maintains all kinds of alarms registered by the application, timely sets the alarm to be triggered to the alarm device (in the system, the device name implemented by Linux is "/ dev / alarm"), and always monitors the alarm device, Once an alarm is triggered or an alarm event occurs, the alarmmanagerservie service program will traverse the alarm list, find the corresponding registered alarm and broadcast. The service program is used by the system service program system at system startup_ Service starts and initializes the alarm device (/ dev / alarm). Of course, there is another layer of encapsulation between the alarmmanagerservice in Java layer and the Linux alarm driver interface, that is JNI.

After the application and service are separated by AlarmManager, application developers do not care about specific services, but use such services directly through AlarmManager. This may be the benefit of the customer / service model. AlarmManager and alarmmanagerservie communicate through binder, and they have a many to one relationship.

In the Android system, alarmmanage provides three interfaces and five types of alarm services.

3 interfaces:

Java code:

5 alarm types

Java code:

Note an important parameter, pendingintent. This pendingintent can be said to be a further encapsulation of intent. It includes both the description of intent and the execution of intent behavior (this definition may not be too strict). If intent is compared to an order, pendingintent is more like a person who places an order, because it is responsible for both sending the order and processing after the order is sent, For example, prepare to accept the order goods after sending successfully, resend or cancel the order after sending failed. Developers can call getactivity (context, int, intent, int) getbroadcast (context, int) getservice (context, int)

There are three different ways to get a pendingintent instance.

Getbroadcast -- pendingintent obtained through this function will play a broadcast function, just like calling context The sendbroadcast() function is the same. When the system sends an intent through it, it shall adopt the form of broadcast, and the corresponding intent receiving object will be included in the intent. Of course, this object can be specified when creating pendingintent, or the system can automatically find the behavior processing object through the description of action and category.

Java code:

Getactivity -- pendingintent obtained through this function can directly start a new activity, just like calling context Startactivity (intent) However, it is worth noting that this new activity is no longer an existing activity of the current process. We must use intent in intent FLAG_ ACTIVITY_ NEW_ TASK.

Java code:

Getservice -- the pengdingintent obtained through this function can directly start a new service, just like calling context Same as startservice().

For more Android related content, readers who are interested can view the topics on this site: introduction and advanced tutorial of Android development, summary of Android debugging skills and common problem solutions, summary of Android multimedia operation skills (audio, video, recording, etc.), summary of Android basic component usage, summary of Android view skills Android layout skills summary and Android control usage summary

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>