Android Set Multiple Repeat Alarms
I want to set 3 different alarms repeat everyday.(like 9:00am,1:00pm,6:00pm) And I have created buttons for them. I can set the alarm's time when I click the button. The problem is
Solution 1:
As I understand your question you want to set alarm at the click of a button. Use the below two methods to set alarm and cancel alarm. To repeat alarm use a unique requestcode in getBroadcast and when cancelling use the same requestcode value to cancel the alarm which was initially used to start it.
When the button is clicked call setAlarm method.
publicvoidsetAlarm(Calendar time,int pk) {
AlarmManagermanager= (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
IntentalarmIntent=newIntent(context, AlarmReceiver.class);
PendingIntentpendingIntent= PendingIntent.getBroadcast(context, pk, alarmIntent, 0);
manager.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), pendingIntent);
}
publicvoidcancelAlarm(int pk) {
AlarmManagermanager= (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
IntentalarmIntent=newIntent(context, AlarmReceiver.class);
PendingIntentpendingIntent= PendingIntent.getBroadcast(context, pk, alarmIntent, 0);
manager.cancel(pendingIntent);
}
Solution 2:
Const.java
publicclassConst {
publicstaticfinalint[] ALARM_HOUR_TIME = {9, 9, 9, 9};
publicstaticfinalint[] ALARM_MINUTE_TIME = {29, 30, 31, 32};
}
AlarmManagerUtil.java
publicclassAlarmManagerUtil {
AlarmManager alarmManager;
PendingIntent pendingIntent;
publicvoidinitAlarmNotification(Context context) {
Calendar calendar = getAlarmDate();
if (calendar == null) {
return;
}
Intent myIntent = new Intent(context, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(context, 0, myIntent, 0);
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
publicvoidcancelAlarm(Context context) {
// If the alarm has been set, cancel it.if (alarmManager != null) {
alarmManager.cancel(pendingIntent);
}
// Disable {@code SampleBootReceiver} so that it doesn't automatically restart the// alarm when the device is rebooted.
ComponentName receiver = new ComponentName(context, BootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
private Calendar getAlarmDate() {
Calendar calendar = Calendar.getInstance();
boolean setAlarm = false;
int hour = Const.ALARM_HOUR_TIME[0];
int minute = Const.ALARM_MINUTE_TIME[0];
int currentHour = calendar.get(Calendar.HOUR_OF_DAY);
int currentMinute = calendar.get(Calendar.MINUTE);
for (int i = 0; i < Const.ALARM_HOUR_TIME.length; i++) {
if (currentHour <= Const.ALARM_HOUR_TIME[i] && currentMinute < Const.ALARM_MINUTE_TIME[i] && !setAlarm) {
hour = Const.ALARM_HOUR_TIME[i];
minute = Const.ALARM_MINUTE_TIME[i];
setAlarm = true;
} elseif (i == (Const.ALARM_HOUR_TIME.length - 1) && !setAlarm) {
calendar.add(Calendar.DATE, 1);
hour = Const.ALARM_HOUR_TIME[0];
minute = Const.ALARM_MINUTE_TIME[0];
}
}
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
Log.d("MyAlarm", "Next Alarm: " + hour + ":" + minute);
return calendar;
}
}
AlarmReceiver.java
publicclassAlarmReceiverextendsWakefulBroadcastReceiver {
@OverridepublicvoidonReceive(Context context, Intent intent) {
AlarmManagerUtilalarmUtil=newAlarmManagerUtil();
alarmUtil.initAlarmNotification(context);
createNotification(context, 1);
}
privatestatic PendingIntent criarPendingIntent(
Context ctx, int id) {
IntentresultIntent=newIntent(ctx, MainActivity.class);
TaskStackBuilderstackBuilder= TaskStackBuilder.create(ctx);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
return stackBuilder.getPendingIntent(id, PendingIntent.FLAG_UPDATE_CURRENT);
}
publicstaticvoidcreateNotification(Context ctx, int id) {
BitmaplargeIcon= BitmapFactory.decodeResource(
ctx.getResources(), R.mipmap.ic_launcher);
PendingIntentpitNotificacao= criarPendingIntent(ctx, id);
Calendarcalendar= Calendar.getInstance();
NotificationCompat.BuildermBuilder=newNotificationCompat.Builder(ctx)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("My Alarm")
.setContentText("HOUR: " + calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE))
.setWhen(System.currentTimeMillis())
.setLargeIcon(largeIcon)
.setAutoCancel(true)
.setContentIntent(pitNotificacao)
.setLights(Color.BLUE, 1000, 5000)
.setVibrate(newlong[]{100, 500, 200, 800})
.setNumber(id)
.setSubText("GORIO Engenharia");
NotificationManagerCompatnm= NotificationManagerCompat.from(ctx);
nm.notify(id, mBuilder.build());
}
}
Solution 3:
set your alarm using setRepeat or setInExactRepeat methods
Post a Comment for "Android Set Multiple Repeat Alarms"