Basic usage analysis of AlarmManager in Android

This article describes the basic usage of AlarmManager in Android. Share with you for your reference, as follows:

The function of AlarmManager is explained in the document: broadcast a specified intent for us at a specific time. In short, we set a time, and then when the time comes, the AlarmManager broadcasts an intent for us.

For a deeper understanding of the corresponding AlarmManager, refer to:

https://www.oudahe.com/p/28358/

Android provides four types of alarm clocks:

① ELAPSED_ REALTIME

After the specified delay, the broadcast is sent without waking up the device.

② ELAPSED_ REALTIME_ WAKEUP

After the specified demonstration, send a broadcast and wake up the device. The delay is the time to start the system systemclock Elapsedrealtime() is included. See the code for specific usage.

③ RTC

At the specified time, the broadcast is sent, but the device is not awakened

④ RTC_ WAKEUP

At a specified time, send a broadcast and wake up the device

Methods provided by AlarmManager:

1. Void set (int type, long triggerattime, pendingintent operation) sets an alarm clock

2. Void setrepeating (int type, long interval, pending intent operation) sets an alarm clock that will repeat

3. Void setinexactrepeating (int type, pending intent operation) sets an imprecise version of a repeating alarm clock, which is relatively more power-efficient, because the system may combine several similar alarm clocks into one to reduce the number of device wakeups.

The built-in intervals are:

INTERVAL_ FIFTEEN_ MINUTES INTERVAL_ HALF_ HOUR INTERVAL_ HOUR INTERVAL_ HALF_ DAY INTERVAL_ DAY

If you set it to day, all alarm clocks in the day may be merged.

Cancel a set alarm clock

Set the default time zone of the system. Android is required permission. SET_ TIME_ Zone permissions

Pendingintent: simply put, add a specified action to the intent. For intent, we also need to execute startactivity, startservice or sendbroadcast to make intent useful. Pendingintent includes this action, such as pendingintent Getbroadcast contains the action of sendbroadcast.

Send the specified broadcast after 5S

Send the specified broadcast after 5S, and then repeat the broadcast every 10 seconds

Cancel an alarm clock

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