2014-07-04 88 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數據和我的自定義數據。