Example of user online heartbeat function based on Android AlarmManager

This article illustrates how Android implements the user's online heartbeat function based on AlarmManager. Share with you for your reference, as follows:

Heartbeat is used in instant messaging or other operations to detect whether it is online. The more commonly used is the AlarmManager global timer.

The use mechanism of AlarmManager is called global timer or alarm clock. In fact, its function is a little similar to that of timer. There are two similar usages: (1) execute an operation after a specified length of time (2) execute an operation periodically

The AlarmManager object can be used with intent to regularly start an activity, send a broadcast, or start a service

The following code describes the use of two timing methods in detail:

(1) Perform an operation after a specified length of time

Note: remember to register the receiver in manifest.xml

(2) Perform an operation periodically

Setrepeating() of AlarmManager is equivalent to timer's schedule (task, delay, peroid); The difference is that the timer method specifies how long it will be delayed before the task is executed periodically;

Cancellation of AlarmManager: (note that the cancelled intent must be absolutely consistent with the start intent to support the cancellation of AlarmManager)

More readers interested in Android related content can view the special topics of this site: summary of Android view skills, summary of activity operation skills of Android programming, summary of Android layout skills, introduction and advanced tutorial of Android development, summary of Android resource operation skills and summary of Android control usage

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
分享
二维码
< <上一篇
下一篇>>