This article describes the basic usage of AlarmManager in Android. Share it for your reference, as follows:
The explanation in the AlarmManager function document is: broadcast a specified Intent to us at a specific moment. Simply put, we set a time, and when that time comes, AlarmManager broadcasts an Intent we set.
For a deeper understanding of AlarmManager, please refer to:
https:///article/
Android provides four types of alarm clocks:
① ELAPSED_REALTIME
After the specified delay, the broadcast is sent, but the device is not awakened.
② ELAPSED_REALTIME_WAKEUP
After the specified demonstration, send a broadcast and wake the device up
Delay requires the system startup time () to be counted. The specific usage depends on the code.
③ RTC
At a specified time, the broadcast is sent, but the device is not awakened
④ RTC_WAKEUP
At a specified time, send a broadcast and wake the device up
Methods provided by AlarmManager:
1. void set(int type, long triggerAtTime, PendingIntent operation)
Set an alarm clock
2. void setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation)
Set a repeating alarm clock
3. void setInexactRepeating(int type, long triggerAtTime, long interval, PendingIntent operation)
Setting up an inaccurate version of a repeating alarm is relatively more power-efficient, because the system may combine several similar alarms into one to perform, reducing the number of wake-up times of the device.
Several built-in intervals are:
INTERVAL_FIFTEEN_MINUTES
INTERVAL_HALF_HOUR
INTERVAL_HOUR
INTERVAL_HALF_DAY
INTERVAL_DAY
If you set it to DAY, then it is possible that all alarm clocks during the day will be merged.
void cancel(PendingIntent operation)
Cancel a set alarm clock
void setTimeZone(String timeZone)
Sets the default time zone of the system. Requires .SET_TIME_ZONE permission
// Create Receiver firstpublic class AlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { (context, "alarm", Toast.LENGTH_SHORT).show(); } }
// Declare in manifest that there is no need for intent-filter, we explicitly specify which receiver to send to<receiver android:name="" />
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. For PendingIntent, this action is included, such as the action of sendBroadcast.
Send the specified broadcast after 5 seconds
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(getApplicationContext(), ); int requestCode = 0; PendingIntent pendIntent = (getApplicationContext(), requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT); // Send the broadcast after 5 seconds, only onceint triggerAtTime = () + 5 * 1000; (AlarmManager.ELAPSED_REALTIME, triggerAtTime, pendIntent);
After 5 seconds, the specified broadcast will be sent repeatedly every 10 seconds.
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(getApplicationContext(), ); int requestCode = 0; PendingIntent pendIntent = (getApplicationContext(), requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT); // Send the broadcast after 5 seconds, and then repeat the broadcast every 10 seconds. The broadcasts are sent directly to AlarmReceiverint triggerAtTime = () + 5 * 1000; int interval = 10 * 1000; (AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, interval, pendIntent);
Cancel an alarm clock
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(getApplicationContext(), ); PendingIntent pendIntent = (getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); // The alarm clock that matches the above intent (filterEquals(intent)) will be cancelled(pendIntent);
For more information about Android related content, please check out the topic of this site:Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Android multimedia operation skills summary (audio, video, recording, etc.)》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.