Search code examples
androidgoogle-play-serviceslocation-client

When should I disconnect from Google Play Services in my Android app?


My app uses the Location Client in order to retrieve current location and also route to points placed by user on a map. I am new to the google play services and am wondering as far as best practices go when should I disconnect my LocationClient if ever?


Solution

  • The Android training site suggests two approaches to connect and disconnect the LocationClient. In the Retrieving the Current Location training, the LocationClient is initialized in onCreate, connect() is called in onStart and disconnect() is called in onStop like this (taken from the source code).

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mLocationClient = new LocationClient(this, this, this);
    }
    
    @Override
    public void onStop() {
        mLocationClient.disconnect();
        super.onStop();
    }
    
    @Override
    public void onStart() {
        super.onStart();
        mLocationClient.connect();
    }
    

    Then you can choose when and where to start or stop the location updates.

    In Recognizing the User's Current Activity the LocationClient is initialized each time the user wants to request or stop activity recognition updates. First, connect() is called, then in onConnected, requestActivityUpdates or removeActivityUpdates is called and immediatly after that disconnect() is called. In the sample source code there is a class called DetectionRequester that starts the activity recognition updates:

    public void requestUpdates() {
        getActivityRecognitionClient().connect();
    }
    
    @Override
    private void onConnected() {
        getActivityRecognitionClient().requestActivityUpdates(...);
        getActivityRecognitionClient().disconnect();
    }
    
    @Override
    public void onDisconnected() {
        mActivityRecognitionClient = null;
    }
    
    private ActivityRecognitionClient getActivityRecognitionClient() {
        if (mActivityRecognitionClient == null) {
            mActivityRecognitionClient = new ActivityRecognitionClient(...);
        }
        return mActivityRecognitionClient;
    }
    

    And there is another similar class called DetectionRemover that stops the activity recognition updates.

    Another approach is to have both methods (requestUpdates and removeUpdates) in the same class, like this:

    public enum RequestType {
        START, STOP;
    }
    
    @Override
    public void onConnected(Bundle bundle) {
        switch (mRequestType) {
            case START:
                getActivityRecognitionClient().requestActivityUpdates(...);
                break;
            case STOP:
                getActivityRecognitionClient().removeActivityUpdates(...);
                break;
            default:
                break;
        }
        getActivityRecognitionClient().disconnect();
    }
    
    @Override
    public void onDisconnected() {
        mActivityRecognitionClient = null;
    }
    
    
    public void requestUpdates() {
        mRequestType = RequestType.START;
        getActivityRecognitionClient().connect();
    }
    
    public void removeUpdates() {
        mRequestType = RequestType.STOP;
        getActivityRecognitionClient().connect();
    }
    
    private ActivityRecognitionClient getActivityRecognitionClient() {
        if (mActivityRecognitionClient == null) {
            mActivityRecognitionClient = new ActivityRecognitionClient(...);
        }
        return mActivityRecognitionClient;
    }
    

    The same approach can be used to start and stop location updates.

    I don't know wich of this approaches might be considered best practice, but they are in the sample source code. I personally use the third approach in all my projects. The third approach was in the Activity Recognition sample until recently.