Search code examples
androidbroadcastreceiverandroid-intentalarmmanager

Sending new intent to broadcast receiver gives extras values from previous intent


I am broadcasting a intent which will be received by a broadcast receiver, as application is still running and new intent is fired by Alarm Service but the receiver is showing the previous intent value. As per docs broadcast receiver is no longer active after returning onReceive(), so receiver should show next intent values which is fired by alarm service, but it is not happening, can any one tell correct approach.

This is from activity to broadcast intent:

 Intent intent = new Intent(SCH_ALARM_ACTION);
    intent.setClass(getBaseContext(), SchAlarmReciever.class);
    intent.putExtra("id", maxId);
    PendingIntent pi = PendingIntent.getBroadcast(getBaseContext(),
                0,
                intent,
                0);
   alarmManager.set(AlarmManager.RTC, gc.getTimeInMillis(), pi);

This is broadreceiver:

@Override
    public void onReceive(Context context, Intent data) 
                {
        // TODO Auto-generated method stub

        if(data.getAction().equals(SchedulerActivity.SCH_ALARM_ACTION)){

        int id = data.getIntExtra("id",0);
        Toast.makeText(context, "in receiver "+String.valueOf(id), Toast.LENGTH_LONG).show();
                }

here toast shows id which is sent by first broadcast from alarmservice even when second intent is fired from alarmservice(second time alarm goes off)


Solution

  • Got answer, has to set flag PendingIntent.FLAG_CANCEL_CURRENT while setting pending intent for AlarmService...