4

我試圖實施地理圍欄機制,其中監控地理圍欄,一旦用戶退出當前地理圍欄,當前座標用於創建新的地理圍欄並啓動數據庫查詢以獲取一些數據。未解決的意圖未解僱

我的問題是,掛起的意圖永遠不會被解僱。

從日誌中我可以看到geofences被添加到位置客戶端。然而,在位置改變時沒有待處理的意圖(我將柵欄半徑設置爲2米,並且走過了100米)。我宣佈意向服務的方式有問題嗎?

這是intent服務類。

public class GeoFenceIntentService extends IntentService{ 
    private static final String mIntentName = "GeoFenceIntentService"; 

    public GeoFenceIntentService() { 
     super(mIntentName); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     int transitionType = LocationClient.getGeofenceTransition(intent); 
     Log.e(TAG,"Inside fence handler"); 
     if(transitionType == Geofence.GEOFENCE_TRANSITION_EXIT){ 
      //Query DB here with current co-ords 
      //create new GeoFence 
      Location location = LocationHelper.getInstance(mContext).getLastLocation(); 
      mLat = String.valueOf(location.getLatitude()); 
      mLong = String.valueOf(location.getLongitude()); 
      addGeofenceToMonitor(location); 
      queryDb(); 
     } 
    }  
} 

而且,這裏是我的懸而未決的意圖和地理圍欄添加到位置客戶

addGeofenceToMonitor(Location location){ 
    List<Geofence> list = new ArrayList<Geofence>(); 
    list.add(getNewGeofence(location.getLatitude(), location.getLongitude())); 
    PendingIntent pendingIntent = PendingIntent.getService(mContext, 0, 
             new Intent(mContext,GeoFenceIntentService.class), PendingIntent.FLAG_UPDATE_CURRENT); 

    OnRemoveGeofencesResultListener removeListener = new OnRemoveGeofencesResultListener() { 

     @Override 
     public void onRemoveGeofencesByRequestIdsResult(int statusCode, String[] requestIDs) { 
      //To be used 
     } 

     @Override 
     public void onRemoveGeofencesByPendingIntentResult(int statusCode,PendingIntent pendingIntent) { 
      //Not used 
     } 
    }; 
    LocationHelper.getInstance(mContext).removeGeoFence(mGeofenceRequestIDs, removeListener); 

    OnAddGeofencesResultListener addListener = new OnAddGeofencesResultListener() { 

     @Override 
     public void onAddGeofencesResult(int statusCode, String[] geofenceRequestIds) { 
      if(statusCode != LocationStatusCodes.SUCCESS){ 
       //handle error cases 
      } 
      else 
       Log.i(TAG, "Successfully added Geofence "+geofenceRequestIds[0]+" for monitoring"); 
     } 
    }; 
    LocationHelper.getInstance(mContext).addGeoFence(list, pendingIntent, addListener); 
} 

這裏是清單文件

<service 
android:name="com.myexample.sample.GeoFenceIntentService" 
android:label="@string/app_name" 
android:exported="true"> 
</service> 
+0

您是否試過使用'BroadcastReceiver'作爲您的'PendingIntent'而不是'Service'的目標?如果不是,請嘗試一下,看看是否調用了「onReceive()」。 –

回答

0

Read this片段。 你有沒有檢查過你的位置估計圈?您可以使用模擬位置應用來設置位置以及準確性圈。您的地理圍欄可能太小,無法容納您的位置圈,因此這些事件不會被觸發。

0

Android GeoFences永遠不會啓用GPS(因爲它們的API非常糟糕,而且它們的設備功耗已經如此失控)。您必須設置您的地理圍欄,然後如果要通過GPS進行地理圍欄,請不斷分別調查GPS。

GPS輪詢的處理程序可以爲空,輪詢只存在強制準確的信息到他們糟糕的位置API並反過來觸發圍欄。