Reputation: 186
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
?
Upvotes: 7
Views: 1595
Reputation: 4809
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.
Upvotes: 0
Reputation: 676
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;
...
}
...
}
Upvotes: 0
Reputation: 4347
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.
Upvotes: 3