Kris Kris - 13 days ago 8
Android Question

Accessing GoogleApiClient object in All Activities

This seems like a simple thing that most people would need if they wanted to use Google Plus sign in with their application :s.

In Activity 1:

I sign the user in.

After the sign in, I want to make that user object globally accessible, so I add it to the Application object:

public class GlobalUserAccess extends Application {

private GoogleApiClient mGoogleApiClient;

public GlobalUserAccess(){
mGoogleApiClient = null;
}

public void setClient(GoogleApiClient client){
mGoogleApiClient = client;
}

public GoogleApiClient getClient(){
return mGoogleApiClient;
}
}


By binding it like so:

GlobalUserAccess client = ((GlobalUserAccess) getApplicationContext());
client.setClient(mGoogleApiClient);


However, when I try to access it in Activity 2:

GlobalUserAccess client = ((GlobalUserAccess) getApplicationContext());
String currentUser = Plus.AccountApi.getAccountName(client.getClient());


I get the error:

E/GMPM: getGoogleAppId failed with status: 10


Can someone please fill me in on the proper way to accomplish this? I'd like to have that user object available to all classes and I've spent way too much time on this :|.

Did I mess up somewhere? Ah...

EDIT: client creation code from Activity 1

mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(new Scope(Scopes.PROFILE))
.addScope(new Scope(Scopes.EMAIL))
.build();


I'm using Googles code directly from their Git repository. It successfully signs in and gets the account info in Activity 1.

Answer

Add google play services dependency and manifest file

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

GoogleApiHelper.java

public class GoogleApiHelper implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
        private static final String TAG = GoogleApiHelper.class.getSimpleName();
        Context context;
        GoogleApiClient mGoogleApiClient;

        public GoogleApiHelper(Context context) {
            buildGoogleApiClient();
            connect();
            this.context = context;
        }

        public GoogleApiClient getGoogleApiClient() {
            return this.mGoogleApiClient;
        }

        public void connect() {
            if (mGoogleApiClient != null) {
                mGoogleApiClient.connect();
            }
        }

        public void disconnect() {
            if (mGoogleApiClient != null && mGoogleApiClient.isConnected()){
                mGoogleApiClient.disconnect();
            }
        }

        public boolean isConnected() {
            if (mGoogleApiClient != null) {
                return mGoogleApiClient.isConnected();
            } else {
                return false;
            }
        }

        private void buildGoogleApiClient() {
            mGoogleApiClient = new GoogleApiClient.Builder(context)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(LocationServices.API).build();

        }

        @Override
        public void onConnected(Bundle bundle) {
            //You are connected do what ever you want
            //Like i get last known location
            Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        }

        @Override
        public void onConnectionSuspended(int i) {
            Log.d(TAG, "onConnectionSuspended: googleApiClient.connect()");
            mGoogleApiClient.connect();
        }

        @Override
        public void onConnectionFailed(ConnectionResult connectionResult) {
            Log.d(TAG, "onConnectionFailed: connectionResult.toString() = " + connectionResult.toString());
        }
    }

App.java

public class App extends Application {
    private GoogleApiHelper googleApiHelper;
    private static App mInstance;

    @Override
    public void onCreate() {
        super.onCreate();

        mInstance = this;
        googleApiHelper = new GoogleApiHelper();
    }

    public static synchronized App getInstance() {
        return mInstance;
    }

    public GoogleApiHelper getGoogleApiHelperInstance() {
        return this.googleApiHelper;
    }
    public static GoogleApiHelper getGoogleApiHelper() {
        return getInstance().getGoogleApiHelperInstance();
    }
}

Note: Don't forget to specifying the fully-qualified name of this subclass as the "android:name" attribute in your AndroidManifest.xml's tag.

MainActivity.java

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //Check if connected
        if(App.getGoogleApiHelper.isConnected())
        {
           //Get google api client  
           GoogleApiClient client = App.getGoogleApiHelper.getGoogleApiClient();
        }

    }