2017-07-06 37 views
0

我嘗試在Android上使用新的簡化位置API(在GPS 11.0中引入)。我編寫了一個實現位置API的服務。但它只觸發併發送位置幾秒(僅2次)。FusedLocationProviderClient未發送位置更新

public class LocationService extends Service { 
public static final String TAG = makeLogTag("LocationService"); 

private static final float LOCATION_TRACKING_MIN_DISTANCE = 15f; 
private static final float ACCEPTED_ACCURACY = 50f; 

private final IBinder mBinder = new LocalBinder(); 

// fused location api 
private FusedLocationProviderClient mFusedLocationClient; 
private LocationRequest mLocationRequest; 
private LocationCallback mLocationCallback; 

@Override 
public void onCreate() { 
    super.onCreate(); 
    Log.i(TAG, "onCreate"); 
    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this); 
    createLocationCallback(); 
    createLocationRequest(); 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    Log.i(TAG, "onStartCommand"); 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     stopSelf(); 
     return START_NOT_STICKY; 
    } 

    getLastKnownLocation(); 
    startPeriodicLocationUpdate(); 

    return START_STICKY; 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    stopPeriodicLocationUpdate(); 
    Log.i(TAG, "onDestroy - Estou sendo destruido "); 
} 

@Nullable 
@Override 
public IBinder onBind(Intent intent) { 
    return mBinder; 
} 

private void getLastKnownLocation() { 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     return; 
    } 
    mFusedLocationClient.getLastLocation() 
      .addOnCompleteListener(task -> { 
       if (task.isSuccessful() && task.getResult() != null) { 
        Location lastLocation = task.getResult(); 
        storeLocation(lastLocation); 
       } 
      }); 
} 

private void createLocationRequest() { 
    mLocationRequest = new LocationRequest() 
      .setInterval(TimeUnit.SECONDS.toMillis(Params.getAgentLocationTrackingUpdateInterval())) 
      .setFastestInterval(TimeUnit.SECONDS.toMillis(Params.getAgentLocationTrackingUpdateInterval())) 
      .setExpirationDuration(TimeUnit.SECONDS.toMillis(10)) 
      .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
} 

private void createLocationCallback() { 
    mLocationCallback = new LocationCallback() { 
     @Override 
     public void onLocationResult(LocationResult locationResult) { 
      super.onLocationResult(locationResult); 
      Location location = locationResult.getLastLocation(); 
      if (location != null) { 
       storeLocation(location); 
      } 

      Log.i(TAG, "locationResult start"); 
      for (Location location1 : locationResult.getLocations()) { 
       Log.i(TAG, "locationResult location " + location1.getLatitude() + " " + location1.getLongitude() + " " + location1.getAccuracy() + " " + location1.getProvider()); 
      } 
      Log.i(TAG, "locationResult stop"); 
     } 
    }; 
} 

private void startPeriodicLocationUpdate() { 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     return; 
    } 

    Log.i(TAG, "periodic update"); 
    mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper()); 
} 

private void stopPeriodicLocationUpdate() { 
    mFusedLocationClient.removeLocationUpdates(mLocationCallback) 
      .addOnCompleteListener(task -> LOGI(TAG, "removed location updates!")); 
} 

private void storeLocation(Location location) { 
    Log.i(TAG, "periodic location " + location.getLatitude() + " " + location.getLongitude() + " " + location.getAccuracy() + " " + location.getProvider()); 
    if (location.getAccuracy() <= ACCEPTED_ACCURACY) { 
     Log.i(TAG, "storeLocation: " + "saved!"); 
     AgentLocationVo agentLocation = new AgentLocationVo(); 
     agentLocation.setDate(DateHelper.getDate(location.getTime())); 
     agentLocation.setLatitude(location.getLatitude()); 
     agentLocation.setLongitude(location.getLongitude()); 
     agentLocation.setAccuracy(location.getAccuracy()); 
     agentLocation.setSyncState(SyncState.NOTUPLOADED); 
     if (!AgentLocationDao.isAgentLocationExist(agentLocation)) 
      AgentLocationDao.insert(agentLocation); 
    } 
} 

@Override 
public void onTaskRemoved(Intent rootIntent) { 
    Log.i(TAG, "onTaskRemoved: "); 
    stopSelf(); 
    JobDispatcher.stopSendLocationJob(getApplicationContext()); // TODO: 04/07/17 not the best place for it 
} 

public static void start(Context context) { 
    context.startService(new Intent(context, LocationService.class)); 
} 

public static void stop(Context context) { 
    context.stopService(new Intent(context, LocationService.class)); 
} 

/** 
* Class used for the client Binder. Because we know this service always 
* runs in the same process as its clients, we don't need to deal with IPC. 
*/ 
public class LocalBinder extends Binder { 
    public LocationService getService() { 
     // Return this instance of LocationService so clients can call public methods 
     return LocationService.this; 
    } 
} 

}

這裏是我用來創建服務的資源: https://android-developers.googleblog.com/2017/06/reduce-friction-with-new-location-apis.html

https://github.com/googlesamples/android-play-location/blob/master/LocationUpdates/app/src/main/java/com/google/android/gms/location/sample/locationupdates/MainActivity.java

https://blog.stylingandroid.com/locationservices/

我一直在使用方法的版本,甚至試圖用待定意圖,但獲得相同的結果。即使應用程序在前臺。

回答

1

原因是該行

.setExpirationDuration(TimeUnit.SECONDS.toMillis(10)) 
+0

這是怎麼解決這個問題? – bkach

+0

@bkach LocationRequest在10秒後過期 –