Pergunta

i ve been facing some problems trying to pass data through intents and pending intents to a BroadcastReceiver, concerning proximity alerts. More specifically, am trying to pass an object, that among others holds the user's constantly changing position. I ve tried various tactics being proposed here (and not only) but none worked, resulting to either null values or same-as-first-time created intents, when the intent is retrieved on the BroadcastReceiver's side. Tactics used:

  • Flagging the intent that carries the object with:FLAG_ACTIVITY_NEW_TASK+FLAG_ACTIVITY_CLEAR_TOP+FLAG_ACTIVITY_SINGLE_TOP Result:Null values on the BroadacastReceiver's side
  • Flagging the pending intent created using the initial intent, with:FLAG_UPDATE_CURRENT or FLAG_CANCEL_CURRENT Result:Null values on the BroadacastReceiver's side
  • Acquiring a random ID for intent or the pending intent using System.currentTimeMillis(); Result:Intents are not fired or received at all
  • Nothing described above. Result:Retrieving the same initial value every time.

Code for the calling method (stripped from any experimentations/producing 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's position
  myService.setMyDriverLng(userLng);//setting user's 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 the BroadcastReceiver's 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.

Foi útil?

Solução

Hmm i think i ve found something:

I placed the BroadcastReceiver (ProximityAlerReceiver), used to detect proximity alerts in the same class (MyCarTracking.class), where the LocationListener.class is located. This, provides immediate access to fresh location updates, creating a new intent wrapped in a new pendingIntent to be fired to the BroadcastReceiver (only when the proximity criteria are met). flags:FLAG_ACTIVITY_NEW_TASK+FLAG_ACTIVITY_SINGLE_TOP and FLAG_CANCEL_CURRENT on intent and pendingIntent, were kept respectively. More specifically:

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 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 producing fresh intents with fresh location updates. Thank you all for your help and your interest :)

Outras dicas

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

I've been struggling with this problem as well. It took me a whole night to find that a weird bug I had was related to this issue.

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

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

I'm also using FLAG_CANCEL_CURRENT on my intents and making sure only pendingintents that share the same purpose get the same data, action and uri.

Hope it helps a little bit.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top