2017-05-31 73 views
1

我正在使用Google Geofence API的Android應用程序。 我的代碼似乎在應用程序處於前景時正常工作,但只要應用程序移至暫停狀態,地理柵欄廣播接收器就會停止接收事件。 我非常確定,當應用移動到背景時,地理柵欄會被刪除,但我不知道爲什麼會發生這種情況。Android地理柵欄在應用程序未運行時停止工作

Breafly,我創建地理圍欄

Geofence geofence = new Geofence.Builder() 
      .setRequestId(id) 
      .setCircularRegion(lat, lng, GEOFENCE_RADIUS) 
      .setExpirationDuration(Geofence.NEVER_EXPIRE) 
      .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_DWELL) 
      .setLoiteringDelay(1000) 
      .build(); 
     geofences.add(geofence); 

的名單,然後我創建了一個請求

geofenceRequest = new GeofencingRequest.Builder() 
     .setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_DWELL) 
     .addGeofences(geofences) 
     .build(); 

在這一點上,我添加了地理圍欄

LocationServices.GeofencingApi.addGeofences(googleApiClient, request, createGeofencePendingIntent()).setResultCallback(this); 

最後我開始廣播接收機

String GEOFENCE_ACTION = "gac.broadcast.geofence"; 
    if (geoFencePendingIntent != null) 
    { 
     GoogleApiClientManager.setGeofencePendingIntent(geoFencePendingIntent); 
     return geoFencePendingIntent; 
    } 
    Intent intent = new Intent(GEOFENCE_ACTION); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), GEOFENCE_REQ_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
    GoogleApiClientManager.setGeofencePendingIntent(pendingIntent); 
    return pendingIntent; 

正如我已經說過的,在前臺運行時,一切工作都很完美,但只要應用移動到後臺,geofence事件就會停止發送給接收方。

附加INFOS(廣播接收器的問題不相關): 我試圖做到以下幾點:

  • 我開始在地圖片段和地理圍欄工作正常,因此應用程序顯示一個通知
  • 我移動到主屏幕,所以應用程序移動到暫停狀態,並且不再收到通知
  • 我回到應用程序檢查位置更新和谷歌API客戶端仍在工作,他們實際上工作
  • 最後地理圍欄無法正常工作

希望一切都很清楚,並且您的某個人可以幫助我。 在此先感謝

回答

0

實際上問題是定義接收方的意圖。

我改變

Intent intent = new Intent(GEOFENCE_ACTION); 

Intent intent = new Intent(context, GeofenceBroadcastReceiver.class); 

現在接收機在背景和前景通知。 希望這可以幫助別人。

+0

你確定它在後臺工作嗎?因爲我遇到了同樣的問題,所以在應用程序處於前臺狀態時工作良好,但當應用程序在後臺運行時,我沒有收到任何通知。而且我已經使用Context和Receiver類設置了這個意圖。 –

相關問題