2017-06-18 88 views
0

我有一個應用程序,使用谷歌地圖geofencing。 (每當用戶穿過圍欄時,會觸發處理該事件的PendingIntentandroid更新位置,即使當應用程序在後臺

問題是,我似乎無法讓我的應用程序在後臺更新位置。

我當前的代碼看起來像這樣

protected void createLocationRequest() { 
     LocationRequest mLocationRequest = new LocationRequest(); 
     mLocationRequest.setInterval(INTERVAL_IN_MILLISECONDS); 
     mLocationRequest.setFastestInterval(FASTEST_INTERVAL_MILLISECONDS); 
     mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 

     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      Toast.makeText(this, "user permissions have blocked the app from getting location updates", Toast.LENGTH_SHORT).show(); 
      return; 
     } 
     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
    } 

在我的活動,與更改偵聽器。

@Override 
    public void onLocationChanged(Location location) { 
     Toast.makeText(this, "testing message, at location " + location, Toast.LENGTH_SHORT).show(); 
    } 

我越來越位置更新,而應用程序是在前臺,爲明顯的OnLocationChanged和我的地理圍欄都被正確觸發。但是當應用程序處於後臺時,我的地理圍欄在我進入應用程序之前不會被觸發。

我甚至不需要回撥,只需要手機檢查它的當前位置。

任何方式獲取位置更新在後臺以及?

回答

相關問題