sdelatorre sdelatorre - 1 year ago 89
Android Question

AlarmManager with BroadCastReceiver: Programed two alarms but received only one

I have two alarms and a broadcastreceiver:

AlarmManager am2=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent2 = new Intent(context, MyReceiverAlarm.class);
intent2.putExtra("name", "juan");
PendingIntent pi2 = PendingIntent.getBroadcast(context, 0, intent2, 0);

am2.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+1000, pi2);

AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, MyReceiverAlarm.class);
intent.putExtra("name", "jose");
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
am.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+8000, pi);

However, I'm always receiving the first one, only. I always get "juan" and the alarm is fires after 8 seconds.
I don't understand the reason...If i have two alarms why fires one ?

My BroadcastReceiver:

Toast.makeText(context, extras.getString("nombre") + "", Toast.LENGTH_LONG).show();
String name=extras.getString("nombre");
if (extras != null && name.equals("juan") {
String callForwardString = PHONE ;
Intent intentCallForward = new Intent(Intent.ACTION_CALL);

Uri uri2 = Uri.fromParts("tel", callForwardString, "#");

} else {

Is not possible to program two alarms?

Thanks in advance

Answer Source

Try to change the request code (second parameter passed to getBroadcast():

PendingIntent pi2 = PendingIntent.getBroadcast(context, 2, intent2, 0);
PendingIntent pi = PendingIntent.getBroadcast(context, 1, intent, 0);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download