2

My Situation:

  1. First, I implemented Google Plus authentication in my app. I followed the quick start instructions and added the quick start sample app code to my app.
  2. Then I wanted to obtain the user's last known location. The Fused Location Provider appeared to be the most modern way of obtaining it, so I looked at LocationUpdates.zip and kpbird's demo code.

My Concern:

  • The com.google.android.gms.common.api.GoogleApiClient and com.google.android.gms.common.GooglePlayServicesClient namespaces introduce some overlap by virtue of the fact that if you want to use GoogleApiClient and LocationClient, then your class (i.e. Activity) must implement the following:

    GoogleApiClient.ConnectionCallbacks,  
    GoogleApiClient.OnConnectionFailedListener,  
    GooglePlayServicesClient.ConnectionCallbacks,  
    GooglePlayServicesClient.OnConnectionFailedListener
    

And code from both namespaces will override the following:

@Override
public void onConnected(Bundle connectionHint) {
            /* pseudo-code
            if (GoogleApiClient) {
               // Implementation
            } else {                
               // Must be LocationClient
            }
            */
}

@Override
public void onConnectionFailed(ConnectionResult result) {
            /* pseudo-code
            if (GoogleApiClient) {
               // Implementation
            } else {                
               // Must be LocationClient
            }
            */
}

Such that you will be forced to write code to discern if it was the GoogleApiClient or the LocationClient that triggered the onConnected and the onConnectionFailed event handlers.


My Question:

Jim G.
  • 15,141
  • 22
  • 103
  • 166
  • Take a look at http://stackoverflow.com/questions/31734567/separating-the-concerns-of-activity-and-googleapiclient/31734568#31734568 It is not a complete solution to your problem, but may give you some ideas. – JP Ventura Jul 30 '15 at 21:48

1 Answers1

5

Instead of implements these interfaces in your activity class itself, you can create separate object members in your activity class.

public YourActivity extends Activity {
    ...
    private GoogleApiClient.ConnectionCallbacks apiClient1 = new GoogleApiClient.ConnectionCallbacks() {
        @Override
        public void onConnectionSuspended(int cause) {
        }

        @Override
        public void onConnected(Bundle connectionHint) {
        }
    };

    private GoogleApiClient.OnConnectionFailedListener apiClient2 = new GoogleApiClient.OnConnectionFailedListener() {
        @Override
        public void onConnectionFailed(ConnectionResult result) {
        }
    };

    private GooglePlayServicesClient.ConnectionCallbacks servicesClient1 = new GooglePlayServicesClient.ConnectionCallbacks() {
        @Override
        public void onDisconnected() {
        }

        @Override
        public void onConnected(Bundle connectionHint) {
        }
    };

    private GooglePlayServicesClient.OnConnectionFailedListener servicesClient2 = new GooglePlayServicesClient.OnConnectionFailedListener() {
        @Override
        public void onConnectionFailed(ConnectionResult result) {
        }
    };
    ...
}

And then, create your GoogleApiClient with

GoogleApiClient.Builder builder = new GoogleApiClient.Builder(yourContext, apiClient1, apiClient2);
builder.addApi(...).addScope(...);
GoogleApiClient apiClient = builder.build();

create your LocationClient with

LocationClient locClient = new LocationClient(yourContent, servicesClient1, servicesClient2);
正宗白布鞋
  • 929
  • 6
  • 13