android – Geofence PendingIntent with extras

前端之家收集整理的这篇文章主要介绍了android – Geofence PendingIntent with extras前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是问题所在:

添加地理围栏的服务:

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

    ...
}

如果我尝试将getEera(…,deal)放入getPendingIntent方法,我会得到List< Geofence> triggeredGeofences = LocationClient.getTriggeringGeofences(intent)== NULL.

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

我怎样才能传递额外的东西,还是从LocationClient获得额外的东西?

解决方法

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

猜你在找的Android相关文章