AlarmManager includes the following methods:
[Java]
// Cancel the registered timer that matches the Parameter
Void cancel (PendingIntent operation)
// Register a new delay timer
Void set (int type, long triggerAtTime, PendingIntent operation)
// Register a repeat type timer
Void setRepeating (int type, long triggerAtTime, long interval, PendingIntent operation)
// Register a non-precision repetition type timer
Void setInexactRepeating (int type, long triggerAtTime, long interval, PendingIntent operation)
// Set the time zone
Void setTimeZone (String timeZone)
Main timer types:
[Java]
Public static final int ELAPSED_REALTIME
// When the system is sleep, this type of alarm will not wake up the system. It is not passed until the system is wake up next time. The time used for this alarm is relative time, starting from the start of the system, including during sleep. You can call SystemClock. elapsedRealtime. The system value is 3 (0x00000003 ).
Public static final int ELAPSED_REALTIME_WAKEUP
// Wake up the system. The usage is the same as ELAPSED_REALTIME. The system value is 2 (0x00000002 ).
Public static final int RTC
// When the system is sleep, this type of alarm will not wake up the system. It is not passed until the System is wake up next time. The time used for this alarm is absolute time and UTC time. It can be obtained by calling System. currentTimeMillis. The system value is 1 (0x00000001 ).
Public static final int RTC_WAKEUP
// Wake up the system. The usage is the same as that of RTC. The system value is 0 (0x00000000 ).
Public static final int POWER_OFF_WAKEUP
// Wake up the system. It is a Shutdown Alarm, that is, the device can also wake up the system when it is shut down, so we call it a Shutdown Alarm. The usage is the same as that of RTC. The system value is 4 (0x00000004 ).
Next we will create a simple alarm program. Start a broadcast at startup, and then drive an alarm to enable it once every one second.
Public class test_chargeReceive extends BroadcastReceiver {
Private static AlarmManager mAlarmManager = null;
Private static PendingIntent mIdleIntent;
Private static final int IDLE_REQUEST = 0;
Static long now = 0;
Static AlertDialog dialog = null;
@ Override
Public void onReceive (Context context, Intent intent ){
If (mAlarmManager = null ){
MAlarmManager = (AlarmManager) context. getSystemService (Context. ALARM_SERVICE );
Intent idleIntent = new Intent ("com. keenhi. charge", null );
MIdleIntent = PendingIntent. getBroadcast (context, IDLE_REQUEST, idleIntent, 0 );
// MAlarmManager. cancel (mIdleIntent );
MAlarmManager. set (AlarmManager. RTC_WAKEUP, System. currentTimeMillis () + 1000, mIdleIntent );
}
String action = intent. getAction ();
If (action. equals ("com. keenhi. charge ")){
MAlarmManager. set (AlarmManager. RTC_WAKEUP, System. currentTimeMillis () + 1000, mIdleIntent );
Now = System. currentTimeMillis ();
Log. I ("alrm timer now =" + now );
}
}
Important manifest. xml code:
<Explorer android: name = "test_chargeReceive">
<Intent-filter>
<Action android: name = "android. intent. action. BOOT_COMPLETED"/>
<Action android: name = "com. keenhi. charge"/>
</Intent-filter>
</Cycler>
The functions of the above program are very simple. First, let it receive "android. intent. action. BOOT_COMPLETED. keenhi. charge ". In order to make it simple, we can display the current number of milliseconds of a system after receiving the message.
The above alarm is one time, so we start alarm again every time we execute it. In this way, we want to stop and the interval can be changed freely. If we only need to implement a duplicate alarm, we can do this.
MAlarmManager. setInexactRepeating
(AlarmManager. RTC_WAKEUP, System. currentTimeMillis (), 1000, mIdleIntent );
You can set a duplicate alarm. You do not need to assign values repeatedly in the future.
Author: wantianpei