Geofence PendingIntent with advanced features

Here is the problem:

A service that adds geo objects:

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 an Intent that passes some data and runs 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 a method getPendingIntent, I got List<Geofence> triggeredGeofences = LocationClient.getTriggeringGeofences(intent) == NULL.

If I do not pass, everything will be fine.

How can I transfer my additional information and still receive additional information from LocationClient?

+4
source share
1 answer

, , . , , GeofenceEvent Serializable . , , , :

:

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;
  }
}

:

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

:

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

, . GeofenceEvent .

+1

Source: https://habr.com/ru/post/1547147/


All Articles