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)
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.
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.
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.
Got answer, has to set flag PendingIntent.FLAG_CANCEL_CURRENT while setting pending intent for AlarmService...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With