0

I write bellow method for alarm.

 public void alarm(int time){
    Intent intent = new Intent(MainActivity.this, Alarm.class);

    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 0 , intent, 0);
    am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+time*1000, pi);
}

This method working perfectly. But the problem when I call the method more than one time. Like,

alarm(10);
alarm(50);

This time it only invoke alarm(10); But don't invoke alarm(50);

Anyone please help why it show this problem!

Zoe
  • 27,060
  • 21
  • 118
  • 148
shariful islam
  • 389
  • 4
  • 18

5 Answers5

2

Your PendingIntent has the same properties both times you call your method. When you call PendingIntent.getBroadcast() the second time it will return the PendingIntent that was already created the first time.

If you want the alarm to trigger twice you need to do something like this:

public void alarm(int time, int requestCode){
    Intent intent = new Intent(MainActivity.this, Alarm.class);

    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), requestCode , intent, 0);
    am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+time*1000, pi);
}

And then call it like this:

alarm(10, 0);
alarm(50, 1);
Sander
  • 808
  • 6
  • 18
  • Hello Sander, thank you so much for your response, was a really useful code. You saved my life, I was in troubles in my job for a issue with that but you are my hero. Thanks again! – Jhon Lemmon Jun 19 '18 at 20:48
1

You are giving the same ID (0) You need to use different unique id. use different pending intent id like this. you can use millisecond as id.

public void alarm(int time, int requestCode){
    Intent intent = new Intent(MainActivity.this, Alarm.class);
    Long timeToMilliSeconds = timeToMilliSeconds(hour, minute);

    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), timeToMilliSeconds , intent, 0);
    am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+time*1000, pi);
}
Hoque MD Zahidul
  • 10,560
  • 2
  • 37
  • 40
0

You are giving the same ID 0 (the second parameter in PendingIntent::getBroadcast) to more than onePendingIntent. This is your mistake.

Depending on the FLAG you set, the behavior may vary.

Consider either using a different ID for each PE or one of the standard flags.

Also see:

payloc91
  • 3,724
  • 1
  • 17
  • 45
0
// context variable contains your `Context`
AlarmManager mgrAlarm = (AlarmManager) context.getSystemService(ALARM_SERVICE);
ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();

for(i = 0; i < 10; ++i)
{
   Intent intent = new Intent(context, OnAlarmReceiver.class);
   // Loop counter `i` is used as a `requestCode`
   PendingIntent pendingIntent = PendingIntent.getBroadcast(context, i, intent, 0);
   // Single alarms in 1, 2, ..., 10 minutes (in `i` minutes)
   mgrAlarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
                SystemClock.elapsedRealtime() + 60000 * i, 
                pendingIntent); 

   intentArray.add(pendingIntent);
}
0

Call it like this

alarm(10, 0);
alarm(50, 1);

For more than one Pendingintent it needs different id so in the caller method pass different id.

David Buck
  • 3,752
  • 35
  • 31
  • 35