从应用程序 android 的另一个 activity 注销 Google Plus?
Logout Google Plus from another activity of the application android?
我在 android 应用程序中集成 Google Plus。我在 登录屏幕 中完成了所有登录过程,在成功场景之后我刚刚完成 activity 并移至下一个 activity,即 家庭活动。我必须从这个 HomeActivity 注销 Google Plus。问题是我已经在 Login Activity 中声明了所有 GoogleApiClient。我在搜索需要 GoogleApiClient 对象的注销时得到一个代码。错误是没有 activity 存在,因为我已经在登录后完成 activity。我的问题是关于你能否建议我知道如果用户没有注销我必须检查并制作他退出了另一个活动?
我的登录Activity代码
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_gplus:
GlobalPreferManager.setString(Keys.SIGN_IN_METHOD, "google_plus");
if (!new ConnectionInfo(getActivity()).isConnectingToInternet()) {
CommonUtils.showToast(getActivity(), "No Internet Connection");
} else {
signIn();
}
break;
}
}
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
}
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
GlobalPreferManager.setBoolean(Keys.IS_LOGGED_IN, true);
GlobalPreferManager.setString(Keys.SIGN_IN_METHOD, "google_plus");
startActivity(new Intent(getActivity(), LocationActivity.class));
getActivity().finish();
} catch (ApiException e) {
CommonUtils.showToast(getActivity(), "Failed to connect!");
}
}
我家Activity代码
@Override
public void onClick(View view) {
Auth.GoogleSignInApi.signOut(LoginFragment.mGoogleApiClient);
}
您所要做的就是将所有 google 集成代码编写在不同的 class GooglePlusLogin
中(您可以随意命名)并通过创建 object
CallingActivity
中的那个 class 使 google 登录,并且您可以通过使 signOut() 可访问来调用 signOut()
方法。
public class GooglePlusLogin implements GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks {
private GoogleApiClient mGoogleApiClient;
FragmentActivity context;
public static final int RESULT_OK = -1;
public static final int RC_SIGN_IN = 9001;
private OnClientConnectedListener listener;
private int type;
public GooglePlusLogin(FragmentActivity context, OnClientConnectedListener listener) {
this.context = context;
this.listener = listener;
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.addScope(new Scope(Scopes.PROFILE))
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RC_SIGN_IN && mGoogleApiClient.isConnected()) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
} else {
signIn(type);
}
}
public void signIn(int type) {
this.type = type;
revokeAccess();
}
private void revokeAccess() {
Auth.GoogleSignInApi.revokeAccess(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
((Activity) context).startActivityForResult(signInIntent, RC_SIGN_IN);
}
});
}
private void handleSignInResult(GoogleSignInResult result) {
Log.d(TAG, "handleSignInResult:" + result.isSuccess());
if (result.isSuccess()) {
GoogleSignInAccount acct = result.getSignInAccount();
String imageUrl = "";
String email = "";
String fullname = "";
if (acct.getPhotoUrl() != null) {
imageUrl = acct.getPhotoUrl().toString();
}
if (acct.getEmail() != null) {
email = acct.getEmail();
}
if (acct.getDisplayName() != null) {
fullname = acct.getDisplayName();
}
acct.getIdToken();
listener.onGoogleProfileFetchComplete(fullname, acct.getId(), email, imageUrl, type);
signOut();
} else {
listener.onClientFailed(type);
signOut();
}
public void onStart() {
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
}
}
public void onStop() {
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d(TAG, "onConnectionFailed:" + connectionResult);
((OnClientConnectedListener) context).onClientFailed(type);
}
@Override
public void onConnected(@Nullable Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
// implement this interface in your `CallingActivity`
public interface OnClientConnectedListener {
void onGoogleProfileFetchComplete(String fullname, String acctId, String email, String imageUrl, int forType);
void onClientFailed(int forType);
}
}
// LoginActivity中的实现
public class LoginActivity extends Activity implements GooglePlusLogin.OnClientConnectedListener {
public GooglePlusLogin plusLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
initViews();
plusLogin = new GooglePlusLogin(this, this);
}
// this is becuase I haven't used enableAutoManage() while connecting to googleApiClient in GooglePLusLogin
@Override
protected void onStart() {
super.onStart();
if (plusLogin != null) {
plusLogin.onStart();
}
}
@Override
protected void onStop() {
super.onStop();
if (plusLogin != null) {
plusLogin.onStop();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GooglePlusLogin.RC_SIGN_IN) {
plusLogin.onActivityResult(requestCode, resultCode, data);
}
}
@Override
public void onGoogleProfileFetchComplete(String fullname, String acctId, String email, String imageUrl, int forType) {
// here you will get all the details of user
plusLogin.signOut();
}
@Override
public void onClientFailed(int forType) {
plusLogin.signOut();
}
}
我在 android 应用程序中集成 Google Plus。我在 登录屏幕 中完成了所有登录过程,在成功场景之后我刚刚完成 activity 并移至下一个 activity,即 家庭活动。我必须从这个 HomeActivity 注销 Google Plus。问题是我已经在 Login Activity 中声明了所有 GoogleApiClient。我在搜索需要 GoogleApiClient 对象的注销时得到一个代码。错误是没有 activity 存在,因为我已经在登录后完成 activity。我的问题是关于你能否建议我知道如果用户没有注销我必须检查并制作他退出了另一个活动?
我的登录Activity代码
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_gplus:
GlobalPreferManager.setString(Keys.SIGN_IN_METHOD, "google_plus");
if (!new ConnectionInfo(getActivity()).isConnectingToInternet()) {
CommonUtils.showToast(getActivity(), "No Internet Connection");
} else {
signIn();
}
break;
}
}
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
}
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
GlobalPreferManager.setBoolean(Keys.IS_LOGGED_IN, true);
GlobalPreferManager.setString(Keys.SIGN_IN_METHOD, "google_plus");
startActivity(new Intent(getActivity(), LocationActivity.class));
getActivity().finish();
} catch (ApiException e) {
CommonUtils.showToast(getActivity(), "Failed to connect!");
}
}
我家Activity代码
@Override
public void onClick(View view) {
Auth.GoogleSignInApi.signOut(LoginFragment.mGoogleApiClient);
}
您所要做的就是将所有 google 集成代码编写在不同的 class GooglePlusLogin
中(您可以随意命名)并通过创建 object
CallingActivity
中的那个 class 使 google 登录,并且您可以通过使 signOut() 可访问来调用 signOut()
方法。
public class GooglePlusLogin implements GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks {
private GoogleApiClient mGoogleApiClient;
FragmentActivity context;
public static final int RESULT_OK = -1;
public static final int RC_SIGN_IN = 9001;
private OnClientConnectedListener listener;
private int type;
public GooglePlusLogin(FragmentActivity context, OnClientConnectedListener listener) {
this.context = context;
this.listener = listener;
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.addScope(new Scope(Scopes.PROFILE))
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RC_SIGN_IN && mGoogleApiClient.isConnected()) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
} else {
signIn(type);
}
}
public void signIn(int type) {
this.type = type;
revokeAccess();
}
private void revokeAccess() {
Auth.GoogleSignInApi.revokeAccess(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
((Activity) context).startActivityForResult(signInIntent, RC_SIGN_IN);
}
});
}
private void handleSignInResult(GoogleSignInResult result) {
Log.d(TAG, "handleSignInResult:" + result.isSuccess());
if (result.isSuccess()) {
GoogleSignInAccount acct = result.getSignInAccount();
String imageUrl = "";
String email = "";
String fullname = "";
if (acct.getPhotoUrl() != null) {
imageUrl = acct.getPhotoUrl().toString();
}
if (acct.getEmail() != null) {
email = acct.getEmail();
}
if (acct.getDisplayName() != null) {
fullname = acct.getDisplayName();
}
acct.getIdToken();
listener.onGoogleProfileFetchComplete(fullname, acct.getId(), email, imageUrl, type);
signOut();
} else {
listener.onClientFailed(type);
signOut();
}
public void onStart() {
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
}
}
public void onStop() {
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d(TAG, "onConnectionFailed:" + connectionResult);
((OnClientConnectedListener) context).onClientFailed(type);
}
@Override
public void onConnected(@Nullable Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
// implement this interface in your `CallingActivity`
public interface OnClientConnectedListener {
void onGoogleProfileFetchComplete(String fullname, String acctId, String email, String imageUrl, int forType);
void onClientFailed(int forType);
}
}
// LoginActivity中的实现
public class LoginActivity extends Activity implements GooglePlusLogin.OnClientConnectedListener {
public GooglePlusLogin plusLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
initViews();
plusLogin = new GooglePlusLogin(this, this);
}
// this is becuase I haven't used enableAutoManage() while connecting to googleApiClient in GooglePLusLogin
@Override
protected void onStart() {
super.onStart();
if (plusLogin != null) {
plusLogin.onStart();
}
}
@Override
protected void onStop() {
super.onStop();
if (plusLogin != null) {
plusLogin.onStop();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GooglePlusLogin.RC_SIGN_IN) {
plusLogin.onActivityResult(requestCode, resultCode, data);
}
}
@Override
public void onGoogleProfileFetchComplete(String fullname, String acctId, String email, String imageUrl, int forType) {
// here you will get all the details of user
plusLogin.signOut();
}
@Override
public void onClientFailed(int forType) {
plusLogin.signOut();
}
}