4

I have some code where a service starts the Google Play Services Location/Activity, when the Activity intent is called, I want to change the setPriority for the Location service.

// Location
           mLocationRequest = LocationRequest.create();
           if (mostProbActName.equals("still")) {
               mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
               Log.i(TAG, "GOING TO BALANCED MODE!");
           } else {
               mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
               Log.i(TAG, "GOING TO HIGH MODE!");
           }

I put the following in the onHandleIntent section, but it doesn't seem that it is changing its behavior. Is there a better way to implement this?

eWizardII
  • 1,916
  • 4
  • 32
  • 55

1 Answers1

0

You have to start and stop the location client for changes to take. The below example code stops and starts the google play services fused location provider and reconnect then in onConnected use the new settings. The principal is similar for the location manager stop and start for new settings.

// only process changes  start is passed in isRecording is global
    if (isRecording != start) {
        isRecording = start;
        if (isRecording) {
            if (locationClient != null && locationClient.isConnected()) {
                locationClient.removeLocationUpdates(locationListener);
                locationClient.disconnect();
            }
            locationClient = null;
            Context context = weakContext.get();
            if (context != null) {
                locationClient = new LocationClient(context,
                        connectionCallbacks,
                        onConnectionFailedListener);
            }
            if (locationClient != null) {
                locationClient.connect();
            }
        } else if (locationClient != null) {
            if (locationClient.isConnected()) {
                locationClient.removeLocationUpdates(locationListener);
                locationClient.disconnect();
            }
            locationClient = null;
        }
    }



@Override
public void onConnected(Bundle arg0) {
    if (locationClient != null) {
        locationClient
                .requestLocationUpdates(
                        LocationRequest
                                .create()
                                .setInterval(locationInterval)
                                .setFastestInterval(locationInterval)
                                .setPriority(
                                        LocationRequest.PRIORITY_HIGH_ACCURACY)
                                .setSmallestDisplacement(recordDistance),
                        locationListener);
        runNotification();
    }
}
danny117
  • 5,581
  • 1
  • 26
  • 35