1 Introduction
Alarm is an independent method of triggering intent at a predetermined time.
Alarm are outside the scope of the application, so they can be used to trigger application events or actions. Once the application is closed, combined with broadcast receiver, they can become particularly powerful, by setting up alarm to start the application or performing actions that the application does not need to open or be active.
For example, you can use alarm to implement an alarm program, perform a normal network query, or schedule a time-consuming or cost-intensive operation at an "off-peak" schedule.
For timed operations that occur only during the application life cycle, the combination of the handler class with the timer and the thread class is a better option that allows Android to better control system resources.
The alarm in Android remains active when the device is in sleep mode, it can be set to wake the device, however, all alarm will be canceled when the device restarts.
2 Implementation Methods
The alarm operation is handled by Alarmmanager, and the system service can be obtained through Getsystemservice, as follows:
Alarmmanager alarms = (Alarmmanager) getsystemservice (Context.alarm_service);
2.1 Adding alarm2.1.1 need to define a calendar object Mcal
Private Calendar mCal = calendar.getinstance ();
2.1.2 Get Alarmmanager
Alarmmanager Alarms = (Alarmmanager) Getsystemservice (Context.alarm_service);
2.1.3 Setting the warning time
//sets the time of this Calendar.
Mcal.settimeinmillis (System.currenttimemillis ());
//sets A field to the specified value.
Mcal.set (Calendar.hour_of_day, Tpicker.getcurrenthour ());
Mcal.set (Calendar.minute, Tpicker.getcurrentminute ());
Mcal.set (Calendar.second, 0);
Mcal.set (Calendar.millisecond, 0);
2.1.4 Registration Intent
There are two points to note: First, why use pendingintent; second, pendingintent get.
//Because alarm is a standalone method that triggers intent at a predetermined time, pendingintent is required and the broadcast class Alarmcaller.class that is responsible for handling the pendingintent needs to be set.
Intent Intent = new Intent (settingsactivity. this, Alarmcaller. class);
Pendingintent Sender = pendingintent.getbroadcast (settingsactivity. This, id, intent, 0);
1) The difference between pendingintent and intent
Intent represents a purpose, the first parameter represents the class, and the second parameter represents the target class. Intent literally means intention, that is, our purpose, the thing we want to do, in activity, we can execute it immediately.
Pendingintent is a description of the intent. Pendingintent is the equivalent of packing for intent, we do not necessarily have to execute it immediately, we will wrap it to other activity or application. At this point, get to pendingintent application can according to the inside of the intent to know the intention of the issuer, choose volley or continue to pass or execute.
2) Acquisition of Pendingintent
Public static Pendingintent Getbroadcast (context context, int requestcode, Intent Intent, int flags)
Retrieve a pendingintent that would perform a broadcast, like calling Context.sendbroadcast ().
Parameters
ContextThe Context in which this pendingintent should perform the broadcast.
RequestcodePrivate request code for the sender (currently not used).
IntentThe Intent to be broadcast.
FlagsMay is Flag_one_shot, Flag_no_create, Flag_cancel_current, flag_update_current, or any of the flags as supported by Intent . FillIn () to control which unspecified parts of the intent so can is supplied when the actual send happens.
Returns
Returns an existing or new pendingintent matching the given parameters. May return NULL if flag_no_create have been supplied.
2.1.5 Registration Alarm
There are 3 ways to register alarm.
1) public void set (int type, long triggerattime, pendingintent operation)
Sets the alarm, but only reminds once.
If there is already a alarm scheduled for the same intentsender, it'll first be canceled.
If the time occurs in the past, the alarm would be triggered immediately.
If there is already a alarm for this Intent scheduled (with the equality of both intents being defined by Filterequals (Int ENT)), then it'll be removed and replaced by this one.
2) public void setinexactrepeating (int type, long triggerattime, long interval, pendingintent operation)
Set alarm, you can repeat the reminder multiple times, the time interval is determined by interval, but this repetition of the reminder is inaccurate.
Schedule a repeating alarm that have inexact trigger time requirements; For example, a alarm that repeats every hour, and not necessarily at the top of the every hour. These alarms is more power-efficient than the strict recurrences supplied by setrepeating (int, long, long, pendingintent) .
3) public void setrepeating (int type, long triggerattime, long interval, pendingintent operation)
Set alarm, you can accurately repeat the reminder multiple times, the time interval is determined by interval.
Schedule a repeating alarm. Note:for timing Operations (ticks, timeouts, etc) it's easier and much more efficient to use Handler.
If there is already a alarm scheduled for the same intentsender, it'll first be canceled.
Like set (int, long, pendingintent), except you can also supply a rate at which the alarm would repeat. This alarm continues repeating until explicitly removed with cancel (pendingintent).
If the time occurs in the past, the alarm would be a triggered immediately, with a alarm count depending on what far in the P AST the trigger time is relative to the repeat interval.
What needs to be explained is the first parameter type of setrepeating, which has 4 main types of alarm:
* Rtc_wakeup
Wakes the device at the specified moment (in UTC format) to trigger intent.
Alarm time in System.currenttimemillis () (Wall clock time in UTC), which would wake up the device when it goes off. Alarm time in System.currenttimemillis () (Wall clock time in UTC), which would wake up the device when it goes off.
* RTC
The intent is triggered at an explicit time (in UTC format), but the device is not awakened.
Alarm time in System.currentTimeMillis()
(wall clock time in UTC). This alarm does isn't wake the device up; If it goes off while the device is asleep, it won't be delivered until the next time the device wakes up.
* Elapsed_realtime
After booting from the device, if the elapsed time reaches the total time, the intent is triggered, but the device is not awakened. The elapsed time includes any time the device sleeps. Note that the calculation point for the time lapse is since it was last started.
Alarm time SystemClock.elapsedRealtime()
in (time since boot, including sleep). This alarm does isn't wake the device up; If it goes off while the device is asleep, it won't be delivered until the next time the device wakes up.
* Elapsed_realtime_wakeup
When the total elapsed time is reached after the device is booted, the device is awakened and intent is triggered if necessary.
Alarm time in Systemclock.elapsedrealtime (time since boot, including sleep), which would wake up the device when it goes Off.
//define the time interval for restarting alarm
int Mtimeinterval = * * * 1000 ;
Alarms.setrepeating (Alarmmanager.rtc_wakeup, Mcal.gettimeinmillis (), mtimeinterval,sender);
2.2 Delete Alarm2.2.1 Get Alarmmanager
Alarmmanager Alarms = (Alarmmanager) Getsystemservice (Context.alarm_service);
2.2.2 Registration Intent
Intent Intent = new Intent (settingsactivity. this, Alarmcaller. class);
Pendingintent Sender = pendingintent.getbroadcast (settingsactivity. This, ID, intent, 0);
2.2.3 Cancel Alarm Registration
Alarms.cancel (sender);
public void Cancel (Pendingintent operation)
Remove any alarms with a matching Intent. Any alarm, the any of the type, whose Intent matches this one (as defined by Filterequals (Intent)), would be canceled.
Parameters
Operation Intentsender which matches a previously added Intentsender.