Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Alarm Manager Test
package com.picodiploma.dicoding.alarmtest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import java.util.Calendar;
public class AlarmReceiver extends BroadcastReceiver {
private Context context;
public AlarmReceiver() {
public AlarmReceiver(Context context) {
this.context = context;
public static final String TYPE = "TYPE";
public static final String TAG = AlarmReceiver.class.getCanonicalName();
public static final String TYPE_DAILY = "DAILY";
public static final String TYPE_RELEASE = "RELEASE";
public static final int ID_DAILY = 1;
public static final int ID_RELEASE = 2;
public void onReceive(Context context, Intent intent) {
String type = intent.getStringExtra(TYPE);
switch (type) {
Log.d(TAG, "onReceive: On Daily invoke");
Log.d(TAG, "onReceive: On Release Invoke");
private Calendar getReminderTime(String type) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 14);
calendar.set(Calendar.MINUTE, type.equals(TYPE_DAILY) ? 10 : 15);
calendar.set(Calendar.SECOND, 0);
return calendar;
private Intent getReminderIntent(String type) {
Intent intent = new Intent(context, AlarmReceiver.class);
intent.putExtra(TYPE, type);
return intent;
public void setReminderDaily() {
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, ID_DAILY, getReminderIntent(TYPE_DAILY), 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, getReminderTime(TYPE_DAILY).getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
public void setReminderRelease() {
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, ID_RELEASE, getReminderIntent(TYPE_RELEASE), 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, getReminderTime(TYPE_RELEASE).getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment