0

的Android設備監視器:FusedLocationProvider發送pendingIntents馬不停蹄

Non stop intents

我的位置更新服務每秒接收意圖十萬。我不知道是什麼觸發它

實例化和呼叫一體化位置,我實現GoogleApiClient.ConnectionCallbacks這樣我就可以啓動定位請求時,我收到onConnected

private GoogleApiClient mGoogleApiClient; 
private boolean mInProgress = false; 
private static int INTERVAL = 10*60*1000; 
private LocationRequest mLocationRequest; 

public void onCreate() { 
    super.onCreate(); 

    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(LocationServices.API) 
      .build(); 

    mLocationRequest = LocationRequest.create() 
      .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) 
      .setSmallestDisplacement(20) 
      .setFastestInterval(INTERVAL) 
      .setInterval(INTERVAL); 

    intent = new Intent(this.getApplicationContext(), LocationUpdateService.class); 
    pendingIntent = PendingIntent.getService(this.getApplicationContext(), 0, intent, 0); 

    Log.d(TAG, "on create"); 
} 

@Override 
public void onConnected(Bundle bundle) { 
    Log.d(TAG, "Connected to FusedLocationAPI"); 
    mInProgress = false; 
    Log.d(TAG, "Fastest interval : " + mLocationRequest.getFastestInterval()); 
    Log.d(TAG, "interval : " + mLocationRequest.getInterval()); 

    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, pendingIntent); 

    Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
    if (location != null) Log.d(TAG, location.toString()); 
} 

接收位置很簡單我的更新服務,它將調用其他服務做一些與位置數據

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    super.onStartCommand(intent, flags, startId); 

    if (LocationResult.hasResult(intent)) { 
     LocationResult locationResult = LocationResult.extractResult(intent); 
     Location location = locationResult.getLastLocation(); 

     sIntent = new Intent(this, DoWork.class); 

     sIntent.putExtra("LATITUDE", location.getLatitude()); 
     sIntent.putExtra("LONGITUDE", location.getLongitude()); 

     startService(intent); 

     Log.d(TAG, "onStartCommand has Intent : " + startId); 
     stopSelf(startId); 
    } else { 
     Log.d(TAG, "onStartCommand no Intent"); 
    } 

    return START_STICKY; 
} 

如果我註釋掉LocationServices.FusedLocationApi.requestLocationUpdates路線,意圖並不如預期發射,所以導致我相信請求構建得很糟糕,或者我沒有正確處理這個意圖,表示我已經處理了它,我找不到任何與之相關的任何內容

回答

0

您是否嘗試更改setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)for PRIORITY_BALANCED_POWER_ACCURACYPRIORITY_LOW_POWER? 此外,我與intentService和覆蓋使其成爲onHandleIntent

@Override 
    protected void onHandleIntent(Intent intent) { 
    if (LocationResult.hasResult(intent)) { 
     LocationResult locationResult = LocationResult.extractResult(intent); 
     Location location = locationResult.getLastLocation(); 
     mLastUpdateTime = DateFormat.getTimeInstance().format(new Date()); 

     if (location != null) { 
      Log.d("locationtesting", "accuracy: " + location.getAccuracy() + " lat: " + location.getLatitude() + " lon: " + location.getLongitude()); 
     } 
    } 
} 
+0

爲什麼精度有任何與報告週期 –

+0

當你設置優先級,你也都在說如何獲取位置(僅適用於WiFi或WiFi& Cell塔,所以你可以更頻繁地獲取更多信息)[google LocationRequest](https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest.html#setPriority(int)) – MatiRC

+0

我之前測試過這款手機時,我的手機已經完全充電 - 當我有機會時,我會試用它。 我不認爲使用intentService會有所作爲嗎? FusedLocationProvider對於接收意圖的內容應該是不可知的 – myTotoro