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

懵懂的女人 提交于 2019-12-10 13:28:55

问题


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)


回答1:


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



来源:https://stackoverflow.com/questions/4836885/sending-new-intent-to-broadcast-receiver-gives-extras-values-from-previous-inten

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!