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.
In your activity 1 when you are trying to get Application object you should do as below:
And then set
GoogleApiClient
object usingsetClient
method. In your activity2 use same way to get Application object.For accessing object from every where, you can do like i did here App.java. Don't forget to add app in manifest like that
now to access object simply do that from any where
Also checkout my GoogleApiHelper class here GoogleApiHelper.java
For GooglePlus Api check out this GooglePlusAPI. Follow these steps.
App Code according to your code
now to initialize and access
Add google play location services dependency and location permission in manifest file
AndroidManifest.xml
app/build.gradle
GoogleApiHelper.java
App.java
Note: Don't forget to specifying the fully-qualified name of this subclass as the "android:name" attribute in your AndroidManifest.xml's tag.
You can get apiClient by callback and get when it will connect
Or you can also get it like this