Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Geofence PendingIntent with extras

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?

like image 336
reel Avatar asked Jul 04 '14 09:07

reel


People also ask

What happens when a geofence expires?

After the geofence expires, Location Services automatically removes it. This lesson shows you how to add and remove geofences, and then listen for geofence transitions using a BroadcastReceiver.

How does pendingintent work in location services?

When Location Services detects that the user has entered or exited a geofence, it sends out the Intent contained in the PendingIntent you included in the request to add geofences.

Why aren't my geofences being triggered when I enter a geofence?

This typically happens after NLP (Android's Network Location Provider) is disabled. If geofences aren't being triggered when the device enters a geofence (the GEOFENCE_TRANSITION_ENTER alert isn’t triggered), first ensure that your geofences are registered properly as described in this guide.

How do I handle a transition into or out of geofence?

In many cases, a BroadcastReceiver is a good way to handle a geofence transition. A BroadcastReceiver gets updates when an event occurs, such as a transition into or out of a geofence, and can start long-running background work. The following snippet shows how to define a PendingIntent that starts a BroadcastReceiver :


1 Answers

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.

like image 56
Lolo Avatar answered Oct 13 '22 02:10

Lolo