Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 521
om252345 Avatar asked Jan 29 '11 12:01

om252345


People also ask

What is the difference between intent and broadcast receiver?

An intent is a messaging object, a broadcast receiver is an app component. An intent is used to request some action from some app component, it could be a broadcast receiver, an activity or a service.

What is intent How are they used to broadcast and receive events?

Broadcasting Events with IntentsSet the action, data, and category of your Intent in a way that lets Broadcast Receivers accurately determine their interest. In this scenario, the Intent action string is used to identify the event being broadcast, so it should be a unique string that identifi es the event.

What is the specific role of intent receiver and intent filter in broadcast receiver class?

BroadcastReceiver : 'Gateway' with which your app tells to Android OS that, your app is interested in receiving information. Intent-Filter : Works with BroadcastReceiver and tells the 'What' information it is interested to receive in. For example, your app wants to receive information on Battery level.


1 Answers

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

like image 66
om252345 Avatar answered Sep 20 '22 18:09

om252345