Geofence PendingIntent with extras

时间:2014-07-04 09:53:39

标签: android google-play-services android-geofence

问题在于:

添加地理围栏的服务:

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

    ...
}

如果我尝试在getPendingIntent方法中添加以下(...,交易),我会List<Geofence> triggeredGeofences = LocationClient.getTriggeringGeofences(intent) == NULL

如果我没有通过额外的一切正常。

如何通过我的额外费用,仍然可以从LocationClient获得额外费用?

3 个答案:

答案 0 :(得分: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数据和我的自定义数据。

答案 1 :(得分:0)

看来GeofenceEvent不能与Serializable的其他目的并存。

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

例如,

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;

        ...
    }

    ...

}

答案 2 :(得分: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很重要,因此请在尝试其他解决方案之前尝试一下。