RTC_WAKEUP
-Wakes up the device to fire the pending intent at the specified time. Wake up the device startup intention at a specific time ELAPSED_REALTIME_WAKEUP examplesHere are some examples of usingELAPSED_REALTIME_WAKEUP
. UseELAPSED_REALTIME_WAKEUP
. Example
Wake up the device to fire the alarm in 30 minutes, and every 30 minutes after that: Wake up the device and start the alarm every 30 minutes
// Hopefully your alarm will have a lower frequency than this!alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, AlarmManager.INTERVAL_HALF_HOUR, AlarmManager.INTERVAL_HALF_HOUR, alarmIntent);
Wake up the device to fire a one-time (non-repeating) alarm in one minute: Wake up the device and start the alarm one minute later, only once
private AlarmManager alarmMgr;private PendingIntent alarmIntent;...alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);Intent intent = new Intent(context, AlarmReceiver.class);alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 60 * 1000, alarmIntent);
RTC examplesHere are some examples of usingRTC_WAKEUP
. UseRTC_WAKEUP
. Example
Wake up the device to fire the alarm at approximately 2: 00 p. m ., and repeat once a day at the same time: at around two o'clock P.M., wake up the device and start the alarm, repeat once a day at the same time
// Set the alarm to start at approximately 2:00 p.m.Calendar calendar = Calendar.getInstance();calendar.setTimeInMillis(System.currentTimeMillis());calendar.set(Calendar.HOUR_OF_DAY, 14);// With setInexactRepeating(), you have to use one of the AlarmManager interval// constants--in this case, AlarmManager.INTERVAL_DAY.alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);
Wake up the device to fire the alarm at precisely a.m., and every 20 minutes thereafter: Wake up the device and start the alarm every 20 minutes after the exact AM.
private AlarmManager alarmMgr;private PendingIntent alarmIntent;...alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);Intent intent = new Intent(context, AlarmReceiver.class);alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);// Set the alarm to start at 8:30 a.m.Calendar calendar = Calendar.getInstance();calendar.setTimeInMillis(System.currentTimeMillis());calendar.set(Calendar.HOUR_OF_DAY, 8);calendar.set(Calendar.MINUTE, 30);// setRepeating() lets you specify a precise custom interval--in this case,// 20 minutes.alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 20, alarmIntent);
Decide how precise your alarm needs to be determines the exact number of your alarmsAs described abve, choosing the alarm type is often the first step in creating an alarm. A further distinction is how precise you need your alarm to be. For most apps,setInexactRepeating()
Is the right choice. when you use this method, Android synchronizes multiple inexact repeating alarms and fires them at the same time. this operation CES the drain on the battery. as described above, selecting the alarm type is the first step to create an alarm. Another difference is how accurate your alarm is. SetInexactRepeating () is the right choice for most applications. When you use this method, Android synchronizes multiple inaccurate duplicate alarms and triggers them at the same time. This not only reduces the consumption of battery power.
For the rare app that has rigid time requirements-for example, the alarm needs to fire precisely at 8: 30 a.m., and every hour on the hour thereafter-usesetRepeating()
. But you shoshould avoid using exact alarms if possible. For rare applications with rigid time requirements, for example, the alarm must be accurate to AM and use setRepeating () every hour (). However, if possible, you should avoid using precise alarms.
WithsetInexactRepeating()
, You can't specify a custom interval the way you cansetRepeating()
. You have to use one of the interval constants, suchINTERVAL_FIFTEEN_MINUTES
,INTERVAL_DAY
, And so on. SeeAlarmManager
For the complete list. When using the setInexactRepeating () method, you cannot specify a custom interval. You can use setRepeating () to specify it. You must use one of the constant intervals, such as interval_effecteen_minutes and INTERVAL_DAY. See the complete list of AlarmManager.
Cancel an AlarmDepending on your app, you may want to include the ability to cancel the alarm. To cancel an alarm, callcancel()
On the Alarm Manager, passing inPendingIntent
You no longer want to fire. For example: depending on your app, you may want to cancel the alarm. Cancel an alarm, call the cancel () method in the alarm Manager, and pass in the PendingIntent object that you do not want to start again. For example:
// If the alarm has been set, cancel it.if (alarmMgr!= null) { alarmMgr.cancel(alarmIntent);}
Start an Alarm When the Device Boots Device starts an Alarm When it startsBy default, all alarms are canceled when a device shuts down. to prevent this from happening, you can design your application to automatically restart a repeating alarm if the user reboots the device. this ensures thatAlarmManager
Will continue doing its task without the user needing to manually restart the alarm. By default, when the device is disabled, all alarms will be canceled. To prevent this from happening, when a user restarts the device, you can design your program to automatically restart a repeated alarm. This ensures that the alarm manager continues its tasks without Manually restarting the alarm.
Here are the steps: the steps below:
- Set
RECEIVE_BOOT_COMPLETED
Permission in your application's manifest. This allows your app to receiveACTION_BOOT_COMPLETED
That is broadcast after the system finishes booting (this only works if the app has already been launched by the user at least once ). In your application configuration file manifest, addRECEIVE_BOOT_COMPLETED permission. This allows your app to receiveACTION_BOOT_COMPLETED
Event broadcast, which is sent after the system starts (only after your app is started at least once ).
- Implement
BroadcastReceiver
To receive the broadcast: implements a BroadcastReceiver to receive the broadcast.public class SampleBootReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) { // Set the alarm here. } }}
- Add the receiver to your app's manifest file with an intent filter that filters on
ACTION_BOOT_COMPLETED
Action: add this receiver to your application configuration file maiifest and filter it with intent filter.ACTION_BOOT_COMPLETED event
Notice that in the manifest, the boot handler er is setandroid:enabled="false"
. This means that the specified er will not be called unless the application explicitly enables it. this prevents the boot handler er from being called unnecessarily. you can enable a receiver (for example, if the user sets an alarm) as follows: note that in the configuration file, You must set the android: enabled = "false" attribute to start the receiver. This means that the receiver will not be called unless the application explicitly enables it. This prevents unnecessary calls when the receiver is started. You can set a receiver (for example, if you set an alarm) as follows:
ComponentName receiver = new ComponentName(context, SampleBootReceiver.class);PackageManager pm = context.getPackageManager();pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
Once you enable the caller this way, it will stay enabled, even if the user reboots the device. in other words, programmatically enabling the specified er overrides the manifest setting, even guest SS reboots. the specified er will stay enabled until your app disables it. you can disable a Explorer (for example, if the user cancels an alarm) as follows: once this method is enabled, it will remain enabled even if the user restarts the device. In other words, the receiver is programmed to overwrite the manifest, or even restarted. The receiver is enabled until your application disables it. You can disable the receiver (for example, if you cancel the alarm) as follows:
ComponentName receiver = new ComponentName(context, SampleBootReceiver.class);PackageManager pm = context.getPackageManager();pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
If the translation is not good, please correct me. I will be very grateful and modify it in time.