Reputation: 13
I tried to create an alarm which will trigger on button click and will repeat on 6:00 am in the morning. The first one that i made worked and is working properly but when i added another repeating alarm it triggers the previous alarm at the same time. So my question is how can I make a multi-alarm work properly?
The code for Repeating alarm
public void setAlarmRepeating()
{
AlarmManager dog=(AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
Intent alarm_dog=new Intent(this,Dog_alarm.class);
PendingIntent pi1 = PendingIntent.getBroadcast(this, 100,alarm_dog, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 6);
calendar.set(Calendar.MINUTE,0);
calendar.set(Calendar.SECOND, 00);
dog.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi1);
AlarmManager dog1=(AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
Intent alarm_dog1=new Intent(this,Dog_alarm.class);
PendingIntent pi2 = PendingIntent.getBroadcast(this, 200,alarm_dog1, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar1 = Calendar.getInstance();
calendar1.set(Calendar.HOUR_OF_DAY, 7);
calendar1.set(Calendar.MINUTE,0);
calendar1.set(Calendar.SECOND, 00);
dog1.setRepeating(AlarmManager.RTC_WAKEUP,calendar1.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi2);
}
And this is for BroadcastReciever
public class MorningAlarm extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager1=(NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
Intent dog_intent1=new Intent(context,HomeActivity.class);
dog_intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent dog_pending1=PendingIntent.getActivity(context, 100,dog_intent1, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder1=new NotificationCompat.Builder(context)
.setContentIntent(dog_pending1)
.setSmallIcon(android.R.drawable.ic_dialog_alert)
.setContentTitle("Pet Guide 101")
.setContentText("Time to feed your dog")
.setAutoCancel(true);
notificationManager1.notify(100,builder1.build());
NotificationManager notificationManager2=(NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
Intent dog_intent2=new Intent(context,HomeActivity.class);
dog_intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent dog_pending2=PendingIntent.getActivity(context, 200,dog_intent2, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder2=new NotificationCompat.Builder(context)
.setContentIntent(dog_pending2)
.setSmallIcon(android.R.drawable.ic_dialog_alert)
.setContentTitle("Pet Guide 101")
.setContentText("Time to walk your dog")
.setAutoCancel(true);
notificationManager2.notify(200,builder2.build());
}
}
Upvotes: 1
Views: 335
Reputation: 307
I think this is because your alarm manager is not taking your task as time critical
you need to use setInexactRepeating()
instead of setRepeating()
function.
hope it will work.
Upvotes: 0
Reputation: 527
When you using PendingIntent.FLAG_UPDATE_CURRENT
as flag the current PendingIntent
update to new extras and no new PendingIntent
created.
You must create new PendingIntent
for each alarm.
Use:
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
Remember for canceling alarms you must restart your device or uninstall your app
Upvotes: 1