Summary of using alarm in Android

preface

In Android, we can set some alarm clocks through AlarmManager. Start our service at some specified point in time to handle events.

For example, pull network data every few hours, or remind users at 8 o'clock every day.

When using AlarmManager, the following points will help you better use this feature.

Trigger time increases randomness

If the task triggered by the alarm clock contains a network request, it is recommended to add randomness to the trigger time point.

It is assumed that the network request to query the weather forecast is set at 8 a.m. without increasing randomness. At 8 o'clock, the server will receive a large number of requests, resulting in excessive pressure on the server. If there are enough devices, the server will even be unable to provide services normally.

Therefore, the randomness is increased and these requests are dispersed to different trigger time points. For example, the trigger time of some users is 7 points and that of some users is 8 points. It can be dispersed into more and more random time intervals as needed.

This can effectively stagger the time of these network requests and reduce the pressure on the server, so that the server can provide better and faster services.

Use setinexactrepeating() instead of setrepeating()

When setinexactrepeating() is used, the system can trigger the alarm clocks of multiple applications at the same time, so as to effectively reduce the number of device wakeups. Since Android 4.4, the trigger time of the alarm clock is no longer accurate.

Therefore, in order to reduce power consumption, it is recommended to use setinexactrepeating() when accurate time triggering is not very necessary.

ELAPSED_ Realtime and RTC

ELAPSED_ Realtime is based on the time from system startup to the present, so it has been deleted_ Realtime is suitable for setting the alarm clock that needs to be triggered in the future.

For example, you want to trigger it after half an hour.

RTC is based on the exact time of the current time zone, so it is suitable for setting an alarm clock that needs to be triggered accurately to a certain time of a day.

For example, you want to trigger at 2 p.m.:

Difference between wakeup version and non wakeup version

Wakeup version:

Non wakeup version:

The difference between them is the response when the device screen is off. When the wakeup version is triggered, the device will still wake up when the screen is off, so that necessary operations can be performed. When the wakeup version is not triggered, if the screen is off at this time, the device will not wake up, but will not pass the pendingintent to execute the task until the user or other operations wake up the device.

summary

The above is the whole content of this article. I hope the content of this article can bring some help to Android developers. If you have any questions, you can leave a message. Thank you for your support for programming tips.

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