7

这是问题所在:

添加地理围栏的服务:

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

如您所见,有 Intent 传递一些数据并开始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());
        }

    ...
}

如果我尝试 putExtra(..., Deals) 在getPendingIntent我得到的方法中List<Geofence> triggeredGeofences = LocationClient.getTriggeringGeofences(intent) == NULL

如果我不通过额外的一切工作正常。

我怎样才能通过我的额外,仍然得到额外的LocationClient

4

3 回答 3

3

我知道这是一个老问题,但我遇到了完全相同的症状和问题。基本上,似乎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 数据和我的自定义数据。

于 2016-03-04T18:50:15.630 回答
0

似乎GeofenceEvent无法与Serializable意图中的额外内容共存。

我找到的解决方案是使传递的对象变为static。在IntentService类中,直接访问静态对象。

例如,

地理围栏服务.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;

        ...
    }

    ...

}
于 2018-09-09T17:23:32.787 回答
0

我不是专家,但这可能会帮助你

在您的代码中

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

用这个代替

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

对于那些看到这个并在附加到 PendingIntent 之前拥有 .putExtra 方法的人来说,这PendingIntent.FLAG_UPDATE_CURRENT很重要,所以在转向另一个解决方案之前尝试一下。

于 2018-09-09T17:32:57.993 回答