Alarm

http://developer.android.com/training/scheduling/alarms.html
http://www.compiletimeerror.com/2015/02/how-to-make-alarm-repeat-in-android-code.html#.Vxc-6UJ97IU

Some Example Code: For Set and Cancel with Boot Receiver
// BEGIN_INCLUDE(set_alarm)
/**
* Sets a repeating alarm that runs once a day at approximately 8:30 a.m. When the
* alarm fires, the app broadcasts an Intent to this WakefulBroadcastReceiver.
* @param context
*/
public void setAlarm(Context context) {

alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AutoMovementGPSReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
// Set the alarm’s trigger time to tomorrow 08:30 a.m.
calendar.set(Calendar.DAY_OF_MONTH, getCurrentDay()+1);
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 30);

/*
* If you don’t have precise time requirements, use an inexact repeating alarm
* the minimize the drain on the device battery.
*
* The call below specifies the alarm type, the trigger time, the interval at
* which the alarm is fired, and the alarm’s associated PendingIntent.
* It uses the alarm type RTC_WAKEUP (“Real Time Clock” wake up), which wakes up
* the device and triggers the alarm according to the time of the device’s clock.
*
* Alternatively, you can use the alarm type ELAPSED_REALTIME_WAKEUP to trigger
* an alarm based on how much time has elapsed since the device was booted. This
* is the preferred choice if your alarm is based on elapsed time–for example, if
* you simply want your alarm to fire every 60 minutes. You only need to use
* RTC_WAKEUP if you want your alarm to fire at a particular date/time. Remember
* that clock-based time may not translate well to other locales, and that your
* app’s behavior could be affected by the user changing the device’s time setting.
*
* Here are some examples of ELAPSED_REALTIME_WAKEUP:
*
* // Wake up the device to fire a one-time alarm in one minute.
* alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
* SystemClock.elapsedRealtime() +
* 60*1000, alarmIntent);
*
* // Wake up the device to fire the alarm in 30 minutes, and every 30 minutes
* // after that.
* alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
* AlarmManager.INTERVAL_HALF_HOUR,
* AlarmManager.INTERVAL_HALF_HOUR, alarmIntent);
*/

// Set the alarm to fire at approximately 8:30 a.m., according to the device’s
// clock, and to repeat once a day.
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);

// Enable {@code SampleBootReceiver} to automatically restart the alarm when the
/* device is rebooted.
ComponentName receiver = new ComponentName(context, AutoMovementGPSBootReceiver.class);
PackageManager pm = context.getPackageManager();

pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP); */
}
// END_INCLUDE(set_alarm)

/**
* Cancels the alarm.
* @param context
*/
// BEGIN_INCLUDE(cancel_alarm)
public void cancelAlarm(Context context) {
// If the alarm has been set, cancel it.
if (alarmMgr!= null) {
alarmMgr.cancel(alarmIntent);
}else{
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AutoMovementGPSReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
alarmMgr.cancel(alarmIntent);
}

// Disable {@code SampleBootReceiver} so that it doesn’t automatically restart the
/* alarm when the device is rebooted.
ComponentName receiver = new ComponentName(context, AutoMovementGPSBootReceiver.class);
PackageManager pm = context.getPackageManager();

pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP); */

}
// END_INCLUDE(cancel_alarm)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s