PendingIntents keep caching the same object

I ran into some problems trying to transmit data through intentions and while awaiting BroadcastReceiver intentions regarding proximity alerts. In particular, I am trying to convey an object that, among other things, constantly changes the user's position. I tried the various tactics offered here (and not only), but no one worked, which led to either zero values ​​or the same as the first created intentions, when the intention was received on the side of BroadcastReceiver. Tactics Used:

  • Mark the intention that the object bears: FLAG_ACTIVITY_NEW_TASK + FLAG_ACTIVITY_CLEAR_TOP + FLAG_ACTIVITY_SINGLE_TOP Result: Zero values ​​on the BroadacastReceiver side.
  • A mark of the pending intent created using the original intent, with: FLAG_UPDATE_CURRENT or FLAG_CANCEL_CURRENT Result: Zero values ​​on the BroadacastReceiver side.
  • Getting a random identifier for an intent or pending intent using System.currentTimeMillis (); Result: intentions do not start or are not accepted at all
  • Nothing is described above. Result: getting the same initial value every time.

Code for the calling method (devoid of any experiments / creating null values):

private void setProximityAlert(MyCar myCar) { String locService = Context.LOCATION_SERVICE; LocationManager locationManager; locationManager = (LocationManager)getSystemService(locService); float radius = myCar.getMyCarRadius(); long expiration = myCar.getMyCarExpiration(); myService.setMyDriverLat(userLat);//setting user position myService.setMyDriverLng(userLng);//setting user position Intent intent = new Intent(myCar.getMyCarName()); intent.putExtra("myCar",myCar); PendingIntent proximityIntent = PendingIntent.getBroadcast(this, -1, intent, 0); locationManager.addProximityAlert(myCar.getMyCarLat(), myCar.getMyCarLng(), radius, expiration, proximityIntent); } 

Code for the calling method that sets the intent filter and registers the BroadcastReceiver:

 public void addNewCarPoint (MyCar myCar){ IntentFilter filter = new IntentFilter(myCar.getMyCarName()); registerReceiver(new ProximityAlertReceiver(), filter); setProximityAlert(myCar); } 

Code for BroadcastReceiver side:

 public class ProximityAlertReceiver extends BroadcastReceiver { @Override public void onReceive (Context context, Intent intent) { MyCar myCar=(MyCar)intent.getParcelableExtra("myCar"); driverLoc=(String)Double.toString(myCar.getMyDriverLat()); Toast.makeText(context, userLoc, Toast.LENGTH_SHORT).show(); Intent i = new Intent(context, MyCarDiscoveryPrompt.class); context.startActivity(i);//firing intent } public void intentDataLoader(){ } 

}

Any ideas would be more than welcome. Thank you in advance.

+4
source share
3 answers

Hmm, I think I found something:

I posted the BroadcastReceiver (ProximityAlerReceiver) used to detect proximity alerts in the same class (MyCarTracking.class) where LocationListener.class is located. This one provides immediate access to new location updates, creating a new intent, completed in a new pending event that should be fired in the BroadcastReceiver (only when proximity criteria are met). flags: FLAG_ACTIVITY_NEW_TASK + FLAG_ACTIVITY_SINGLE_TOP and FLAG_CANCEL_CURRENT on intentions and expectations were saved accordingly. More specific:

Code for LocationListener:

 private final LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { updateWithNewLocation(location);//update application based on new location } public void onProviderDisabled(String provider){ updateWithNewLocation(null);//update application if provider disabled } public void onProviderEnabled(String provider){ // Update application if provider enabled } public void onStatusChanged(String provider, int status, Bundle extras){ //update application if provider hardware status changed } }; 

Code for the setProximityAlert () method:

 private void setProximityAlert() { String locService = Context.LOCATION_SERVICE; Context context =getApplicationContext(); LocationManager locationManager; locationManager = (LocationManager)getSystemService(locService); float radius = myCar.getMyCarRadius(); long expiration = myCar.getMyCarExpiration(); Intent intent = new Intent(CAR_DISCOVERED); intent.putExtra("myCar",myCar); locationManager.getLastKnownLocation(provider); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);//flagging intent PendingIntent proximityIntent = PendingIntent.getBroadcast(context, -1, intent, PendingIntent.FLAG_CANCEL_CURRENT);//flagging pendingIntent locationManager.addProximityAlert(myCar.getMyCarLat(), myCar.getMyCarLng(), radius, expiration, proximityIntent);//setting proximity alert } 

This solution works by generating fresh intentions with fresh location updates. Thank you all for your help and your interest :)

+1
source

Try adding intent.setData (URI); where uri is a unique value for each pending intent

0
source

I am also struggling with this problem. It took me a whole night to discover that I had a strange error related to this problem.

Here's a good discussion of Google code on the topic: http://groups.google.com/group/android-developers/browse_thread/thread/b2060b27c8934921

I solved all my problems with (ab) using both uri in SetData and the (reserved) request code in PendingEvent.GetWhatever.

I also use FLAG_CANCEL_CURRENT for my own purposes and certify that only pendingintents who use the same target receive the same data, action and uri.

Hope this helps a bit.

0
source

All Articles