Alex Rivera | Logout

Why the PendingIntent doesn't send back my custom Extras setup for the Intent?

Asked 2010-06-27T16:23:10.130
36

This questions somehow relates to the question when I was looking to get the extras back in startActivityForResult but now I face another challenge.

I have subscribed to receive ProximityAlerts and I have explicitly constructed the Intent to include some Extras. But when I got the service the extras are not there.

After the answers here is the working code:

Intent intent = new Intent(this, PlacesProximityHandlerService.class);
intent.setAction("PlacesProximityHandlerService");
intent.putExtra("lat", objPlace.getLat());
intent.putExtra("lon", objPlace.getLon());
intent.putExtra("error_m", objPlace.getError()+ALERT_RANGE_IN_METERS);
PendingIntent sender=PendingIntent.getService(this, 0, intent, 0);
LocationUtils.addProximity(this, objPlace.getLat(), objPlace.getLon(),objPlace.getError()+ALERT_RANGE_IN_METERS, -1, sender);

The documentation says param PendingIntent to be sent for each location update

Edit
Report

2 Answers

29

If you have multiple outstanding PendingIntents, you need to make sure that the underlying Intents differ on more than their extras. Otherwise, Android will keep reusing the first PendingIntent you created for your first Intent, using that first Intent's extras all of the time.

For example, you could add a unique action via setAction() -- that will not change your Intent routing (since you are specifying the component), but it will make your Intents different.

answered 2010-06-27T17:58:46.077
2

The key is to set the extras and the unique action into the intent before calling

PendingIntent sender=PendingIntent.getService(this, 0, intent, 0);

if you set the extras and action into the intent after calling the above, it won't work. This will not work:

Intent intent;  
PendingIntent sender=PendingIntent.getService(this, 0,   
   intent=new Intent(this, PlacesProximityHandlerService.class), 0);  

intent.setAction("PlacesProximityHandlerService");  
intent.putExtra("lat", objPlace.getLat());  
answered 2011-04-20T14:19:30.580

Your Answer