Geofence PendingIntent with extras
Asked Answered
W

3

7

Here is the problem:

Service that add geofences:

public class GeofenceService extends Service implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener, LocationClient.OnAddGeofencesResultListener, LocationClient.OnRemoveGeofencesResultListener {
     ...

            @Override
                public void onConnected(Bundle bundle) {
                    Log.d(TAG, "onConnected");
                    switch (action){
                        case ADD:
                            Log.d(TAG, "onConnected ADD");
                            locationClient.addGeofences(geofencesToAdd, getPendingIntent(), this);
                            break;
                        case REMOVE:
                            Log.d(TAG, "onConnected REMOVE");
                            locationClient.removeGeofences(geofencesToRemove, this);
                            break;
                    }
                }

                private PendingIntent getPendingIntent(){
                    Intent intent = new Intent().setClass(this, TransitionsIntentService.class);
                    intent.putExtra(EXTRA_DEALS, deals);
                    return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
                }
    ...
}

As you can see there is Intent which pass some data and starts TransitionIntentService:

public class TransitionsIntentService extends IntentService {

   ... 
@Override
protected void onHandleIntent(Intent intent) {
        deals = (ArrayList<Deal>) intent.getSerializableExtra(GeofenceService.EXTRA_DEALS); //THIS CAN BE NULL

        int transitionType = LocationClient.getGeofenceTransition(intent);

        List<Geofence> triggeredGeofences = LocationClient.getTriggeringGeofences(intent); //THIS CAN BE NULL
        List<String> triggeredIds = new ArrayList<String>();

        for (Geofence geofence : triggeredGeofences) {
            Log.d("GEO", "onHandle:" + geofence.getRequestId());
            processGeofence(geofence, transitionType);
            triggeredIds.add(geofence.getRequestId());
        }

    ...
}

If I try to putExtra(..., deals) in getPendingIntent method I'v got List<Geofence> triggeredGeofences = LocationClient.getTriggeringGeofences(intent) == NULL.

If I don't pass extra everything works fine.

How can I pass my extra and still get extra from LocationClient?

Waldner answered 4/7, 2014 at 9:53 Comment(3)
Is only triggeredGeofences null, or deals too in onHandleIntent?Selffertilization
If I pass Deal objects as extra then deals not null. If I don't then triggeredGeofences not null. Also, I can pass String object as extra and everithing will be fine. The problem apears when I try to pass my own Serializable Deal ObjectWaldner
even I have this issue, did you resolve it by any chance?Gatewood
E
3

I know this is an old question, but I was confronted to the exact same symptoms and problems. Basically, it seems the GeofenceEvent cannot coexist with a Serializable extra in the intent. The only solution I found was to flatten the serializable object and use a separate extra with a simple data type for each field instead, as in this simple example:

Object to transfer via intent:

public class MyObject {
  public String stringfield;
  public int intField;

  public MyObject fromIntent(Intent intent) {
    stringField = intent.getStringExtra("stringField");
    intField = intent.getIntExtra("intField", -1);
    return this;
  }

  public MyObject toIntent(Intent intent) {
    intent.putExtra("stringField", stringField);
    intent.putExtra("intField", intField);
    return this;
  }
}

Creating and populating the intent:

MyObject obj = ...;
Intent intent = ...;
myObject.toIntent(intent);

Extracting data from the received intent:

Intent intent = ...;
MyObject obj = new MyObject().fromIntent(intent);

This is a bit cumbersome, but it's the only way I could get it to work. I can now extract both the GeofenceEvent data and my custom data from the same intent.

Endocranium answered 4/3, 2016 at 18:50 Comment(0)
M
0

It seems the GeofenceEvent cannot coexist with a Serializable extra in the intent.

The solution I found was to make the passing object become static. In the IntentService class, access the static object directly.

For example,

GeofenceService.java

public class GeofenceService extends Service implements GooglePlayServicesClient.ConnectionCallbacks, ...

{

     ...

     public static ArrayList<Deal> extraDeals;

     ...

}

TransitionsIntentService.java

public class TransitionsIntentService extends IntentService {

    ... 

    @Override
    protected void onHandleIntent(Intent intent) {
        ...

        deals = GeofenceService.extraDeals;

        ...
    }

    ...

}
Marindamarinduque answered 9/9, 2018 at 17:23 Comment(0)
P
0

I am not Expert But This May Helps You

in Your code

return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

replace with this

return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

For those looking at this and have the .putExtra methods before attaching to PendingIntent, the PendingIntent.FLAG_UPDATE_CURRENT is important, so try that before moving on to another solution.

Puny answered 9/9, 2018 at 17:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.