2014-07-04 90 views
4

这里的问题是:地理围栏的PendingIntent与演员

服务是添加地理围栏:

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

正如你可以看到有意向它通过一些数据,并开始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得到额外的?

+0

'onHandleIntent'中是否只''triggeredGeofences' null或'deals'? – Sassa

+0

如果我将交易对象作为额外交易,则交易不为空。如果我不那么触发Geofences不为null。另外,我可以传递String对象作为额外的,并且永久性会很好。当我试图通过我自己的可序列化交易对象 – reel

+0

,即使我有这个问题,这个问题已经解决了,你是否解决了这个问题? – satheeshwaran

回答

1

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