我已经登录谷歌加,但当我登录成功,并在新活动,我退出,我再次登录后,它不是成功登录。 记录: ClearcutLoggerApiImpl:断开托管的GoogleApiClient
是代码:
GoogleSingleton:
public class GoogleSingleton {
private ILogInGoogleListener mListener = null;
public void setLogInGooglePlusListener(final ILogInGoogleListener listener) {
this.mListener = listener;
}
public interface ILogInGoogleListener {
void logInGooglePlusSuccess(String name,
String email,
String pid);
}
private static GoogleSingleton googleSingleton;
private final GoogleApiClient mGoogleApiClient;
private ConnectionResult mConnectionResult;
public static final int RC_SIGN_IN = 321;
private Activity activity;
private GoogleSingleton(final Activity activity) {
this.activity = activity;
mGoogleApiClient = new GoogleApiClient.Builder(activity)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(final Bundle bundle) {
try {
if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
System.out.println(">>>> Log in xong");
Person currentPerson = Plus.PeopleApi
.getCurrentPerson(mGoogleApiClient);
String personName =
currentPerson.getDisplayName();
String email =
Plus.AccountApi.getAccountName(mGoogleApiClient);
String id = currentPerson.getId();
if (mListener != null) {
mListener.logInGooglePlusSuccess(personName,
email,
id);
}
}
} catch (Exception e) {
e.printStackTrace();
}
// Update the UI after signin
}
@Override
public void onConnectionSuspended(final int i) {
mGoogleApiClient.connect();
System.out.println(">>>> chua log in");
}
})
.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(final ConnectionResult result) {
if (!result.hasResolution()) {
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(),
activity,
0)
.show();
}
mConnectionResult = result;
}
})
.addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN)
.build();
}
public static GoogleSingleton getInstance(Activity activity) {
if (googleSingleton == null) {
googleSingleton = new GoogleSingleton(activity);
}
return googleSingleton;
}
public void connectGoogle() {
mGoogleApiClient.connect();
}
public void disConnectGoogle() {
mGoogleApiClient.disconnect();
}
public boolean isConnecting() {
return mGoogleApiClient.isConnecting();
}
public boolean isConnected() {
return mGoogleApiClient.isConnected();
}
public void login() {
System.out.println(">>>>>> check login Singleton");
if (!mGoogleApiClient.isConnecting()) {
if (mConnectionResult != null && mConnectionResult.hasResolution()) {
System.out.println(">>>>>> check login Singleton Try:" + mGoogleApiClient.isConnected() + "__" + mConnectionResult.hasResolution());
try {
mConnectionResult.startResolutionForResult(activity,
RC_SIGN_IN);
System.out.println(">>>>>> abce");
} catch (IntentSender.SendIntentException e) {
mGoogleApiClient.connect();
}
}
} else {
System.out.println(">>>>>> check login Singleton:" + mGoogleApiClient.isConnected() + "__" + mConnectionResult.hasResolution());
}
}
public void logOut() {
if (mGoogleApiClient.isConnected()) {
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
mGoogleApiClient.disconnect();
mGoogleApiClient.connect();
//set text button google plus.
}
}
}
活动1: 登录:
/**
* init google plus.
* - init api client.
* - fill data when success.
* - set text when login success.
*/
private void initGooglePlus() {
GoogleSingleton.getInstance(this)
.connectGoogle();
GoogleSingleton.getInstance(this)
.setLogInGooglePlusListener(new GoogleSingleton.ILogInGoogleListener() {
@Override
public void logInGooglePlusSuccess(final String name,
final String email,
final String pid) {
System.out.println(">>>>> check name:"+name);
logInGooglePlus(name,
email,
pid);
}
});
}
/**
* - Sign in google.
* - check null button,and check have sign in.
* - when logout,set data model user local empty.
* - if have sign in, click sign out,reverse.
*
* @param view
* This is button google.
*/
@OnClick(R.id.btnDangNhapGoogle)
protected void setOnDangNhapClicked(View view) {
//sign in and sign out google.
if (btnDangNhapGooglePlus != null) {
//log out
if (btnDangNhapGooglePlus.getText()
.equals(getResources().getString(R.string.text_log_out))) {
if (GoogleSingleton.getInstance(this)
.isConnected()) {
//Log out.
GoogleSingleton.getInstance(this)
.logOut();
//set text button google plus.
btnDangNhapGooglePlus.setText(getResources().getString(R.string.text_dang_nhap_bang_google_plus));
//set null user model.
LocalStorage.getShareInstance(SignInActivity.this)
.saveUserModel(new UserModel());
}
}//log in
else {
if (!GoogleSingleton.getInstance(this)
.isConnecting()) {
GoogleSingleton.getInstance(this)
.login();
}
}
}
}
/**
* - activity result of facebook, google plus.
* - when logout from account activity, logout all social, and set local data user empty.
*
* @param requestCode
* This is request.
* @param resultCode
* This is result.
* @param data
* This is data.
*/
@Override
protected void onActivityResult(final int requestCode,
final int resultCode,
final Intent data) {
super.onActivityResult(requestCode,
resultCode,
data);
mCallbackManager.onActivityResult(requestCode,
resultCode,
data);
System.out.println(">>>>>> connect requecode"+requestCode);
if (requestCode == GoogleSingleton.RC_SIGN_IN) {
System.out.println(">>>>>> connect RC_SIGN_IN");
if (!GoogleSingleton.getInstance(this)
.isConnecting()) {
System.out.println(">>>>>> connect vvvvv");
mIsCheckLoginGoogle = true;
GoogleSingleton.getInstance(this)
.connectGoogle();
}
}
if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_CODE_LOGOUT_ALL) {
System.out.println(">>>>>> check result logout code");
logOutFaceBookAndGooglePlusResult();
//set null user model.
LocalStorage.getShareInstance(SignInActivity.this)
.saveUserModel(new UserModel());
}
}
/**
* - check facebook and google plus.
* - logout facebook and google plus.
* - set text google plus.
*/
private void logOutFaceBookAndGooglePlusResult() {
if (GoogleSingleton.getInstance(this)
.isConnected()) {
GoogleSingleton.getInstance(this)
.logOut();
//set text button google plus.
btnDangNhapGooglePlus.setText(getResources().getString(R.string.text_dang_nhap_bang_google_plus));
}
//log out facebook.
if (AccessToken.getCurrentAccessToken() != null) {
LoginManager.getInstance()
.logOut();
}
}
活动2:
//init google connect.
GoogleSingleton.getInstance(this).connectGoogle();
//logout google.
GoogleSingleton.getInstance(this).logOut();