HarshitVaish
10/23/2016 - 6:25 AM

Schedule an alarm with the alarm manager class

Schedule an alarm with the alarm manager class

// Schedule an alarm for 8 a.m. in the morning

// Create a calender object to specify the time alarm has to be triggered
Calendar calendar = Calendar.getInstance();

calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 0);

// Create an intent to indicate which component should be triggered upon the alarm
// being triggered
Intent myIntent = new Intent(context, Receiver.class);
pendingIntent = PendingIntent.getBroadcast(context, 0, myIntent,0);

// Get an alarm manager object from the system service
AlarmManager alarmManager = (AlarmManager)this.getSystemService(ALARM_SERVICE);

// API 19 > non repeating single alarm trigger
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

// Repeating everyday alarm trigger (Won't run immediately)
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY, pendingIntent);