我已经完成了一些与google plus登录和注销相关的stackoverflow问题 . 而且大多数都已经过时了 . 我无法实现我真正想要的 .
退出后,下次登录时,我希望用户选择可用的Google帐户再次登录 .
I am using custom sign-in and sign-out button. 为了退出,我有两个案例,
如果任何用户已在同一登录活动中登录,则在登录前
-
退出 .
-
从不同的活动中退出 .
这是我到目前为止实施的内容:
public class LoginActivity extends AppCompatActivity implements OnClickListener{
private static final int RC_SIGN_IN = 9001;
private GoogleApiClient mGoogleApiClient;
private ConnectionResult mGoogleConnectionResult;
private Button login;
private ProgressDialog mProgressDialog;
private Context mContext;
private String mUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext= LoginActivity.this;
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.login_layout);
login= (Button) findViewById(R.id.lg_login_btn);
login.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(isOnline(mContext)){
if(v.getId()== R.id.lg_login_btn) {
if(mGoogleApiClient!=null){
mGoogleApiClient.disconnect();
}
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(new Scope(Scopes.PLUS_LOGIN))
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(mContext.getApplicationContext())
.enableAutoManage(this , mGPlusConnectionFailedListener)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.addApi(Plus.API)
.build();
signOutGPlus();
Intent lSignInIntent= Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(lSignInIntent, RC_SIGN_IN);
}
} else{
showAlertDialog(mContext, "Error", "Please check internet connection");
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
logD("&&onActivityResult", "requestCode: "+requestCode); // first
if (requestCode == RC_SIGN_IN) {
if(resultCode == RESULT_OK){
showProgressDialog();
getGPlusUserInfo(data);
} else {
logD("&&onActivityResult", "requestCode: RESULT_ NOT Ok"+requestCode);
}
}
}
GoogleApiClient.OnConnectionFailedListener mGPlusConnectionFailedListener= new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
logD("&&mGPlusConnectionFailedListener", "onConnectionFailed");
}
};
private void getGPlusUserInfo(Intent data){
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
private void handleSignInResult(GoogleSignInResult result) {
Log.d("&&handleSignInResult", "handleSignInResult:" + result.isSuccess());
if (result.isSuccess()) {
// Signed in successfully, show authenticated UI.
String lDisplayName="";
String lEmail="";
String lFirstName="";
String lLastName="";
String lGender="";
// G+
if (mGoogleApiClient.hasConnectedApi(Plus.API)) {
logD("&&GPlusUserInfo", "&hasConnectedApi--------------------------------");
// Deprecated
Person person = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
if(null != person) {
logD("&&GPlusUserInfo", "&--------------------------------");
logD("&&GPlusUserInfo", "&Display Name: " + person.getDisplayName());
lDisplayName= person.getDisplayName();
logD("&&GPlusUserInfo", "Gender: " + person.getGender());
if(person.getGender()< MyHalConstants.GENDER.length){
lGender= MyHalConstants.GENDER[person.getGender()];
} else{
lGender= "Other";
}
}
}
GoogleSignInAccount acct = result.getSignInAccount();
if(null != acct) {
if (null != acct.getDisplayName()) {
logD("&&GPlusUserInfo", "&Display Name: " + acct.getDisplayName());
}
lFirstName= acct.getGivenName();
lLastName= acct.getFamilyName();
// Views inside NavigationView's header
Uri uri = acct.getPhotoUrl();
}
} else {
// Signed out, show unauthenticated UI.
signOutGPlus();
}
}
// sign - out
private void signOutGPlus(){
logD("&&signOutGPlus", "signOutGPlus");
if(null != mGoogleApiClient){
mGoogleApiClient.connect();
mGoogleApiClient.registerConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(@Nullable Bundle bundle) {
if(mGoogleApiClient.isConnected()) {
logD("&&signOutGPlus", "inside");
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(@NonNull Status status) {
logD("&&signOutGPlus", "onResult");
if(mGoogleApiClient.isConnected()){
mGoogleApiClient.clearDefaultAccountAndReconnect();
mGoogleApiClient.disconnect();
}
// Deprecated
/*Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
//Plus.AccountApi.revokeAccessAndDisconnect(mGoogleApiClient);
//revokeAccess();*/
}
}
);
}
}
@Override
public void onConnectionSuspended(int i) {
}
});
}
}
// Not used
private void revokeAccess() {
logD("&&revokeAccess", "revokeAccess");
Auth.GoogleSignInApi.revokeAccess(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
// ...
}
});
}
private void showProgressDialog() {
if (mProgressDialog == null) {
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage(getString(R.string.loading));
mProgressDialog.setIndeterminate(true);
}
mProgressDialog.show();
}
private void hideProgressDialog() {
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.hide();
}
}
private void showAlertDialog(Context pContext, String pTitle, String pMessage){
AlertDialog.Builder ldialogBuilder= new AlertDialog.Builder(pContext);
ldialogBuilder.setTitle(pTitle)
.setMessage(pMessage)
.setPositiveButton("Ok", null);
ldialogBuilder.show();
}
private void dismissDialog(){
if(null != mProgressDialog){
mProgressDialog.dismiss();
mProgressDialog= null;
}
}
}
至于从不同活动中退出,我遇到的答案都没有定义如何在新活动中初始化 mGoogleApiClient .
如果我实现以下代码,请注销:
private GoogleApiClient mGoogleApiClient;
// sign - out
private void signOutGPlusFromDifferentActivity(){
logD("&&signOutGPlus", "signOutGPlus");
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(new Scope(Scopes.PLUS_LOGIN))
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(mContext.getApplicationContext())
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.addApi(Plus.API)
.build();
if(null != mGoogleApiClient){
mGoogleApiClient.connect();
mGoogleApiClient.registerConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(@Nullable Bundle bundle) {
if(mGoogleApiClient.isConnected()) {
logD("&&signOutGPlus", "inside");
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(@NonNull Status status) {
logD("&&signOutGPlus", "onResult");
if(mGoogleApiClient.isConnected()){
mGoogleApiClient.clearDefaultAccountAndReconnect();
mGoogleApiClient.disconnect();
}
}
}
);
}
}
@Override
public void onConnectionSuspended(int i) {
}
});
}
}
它抛出错误 .
通过从登录活动中删除注销部分,我可以从GPlus正确登录 .
摇篮:
compile 'com.google.android.gms:play-services-auth:9.2.1'
compile 'com.google.android.gms:play-services:9.2.1'
注意:从登录活动我可以从google plus或face-book登录 .
活动A(从g或fb登录) .
登录后,用户被定向到活动B,来自活动B的用户可以从相应的门户(g或fb)注销 .
Facebook部分已经完成 . 只留下是g .
Please help in signing-out properly in both the case BY USING UPDATED GOOGLE LOGIN LOGOUT APIs.
4 回答
对于注销,您可以使用以下方法:
登录时,您可以按照以下方法操作:
诀窍是 clearDefaultAccountAndReconnect GoogleApiClient 刚刚用户点击Google登录按钮登录 . This ensures that all the available Google accounts are always shown whenever you click on Login Button.
STEP 1: Initialize GoogleAPIClient and set onClickListener to googleSignInButton in Activity onCreate()
STEP 2: Handle the Google Sign in through your onClick() method
MISCALLANEOUS
为什么在代码中需要这么多的回调方法?我想你的代码有点复杂了 . The main trick is to disconnect and clear the default account whenever user tries to login which ensures that he is always shown a list of valid google accounts. 我使用过相同的程序,经过试用,测试和工作 .
我的应用中使用的登录状态只有一种回调方法 .
只需在用户单击“注销”按钮时注销用户并撤消访问权限 . 下次,当用户尝试登录时,他/她将被要求选择他们喜欢的谷歌帐户 .
由于您是从不同的活动登录并注销,因此最终会出现重复的代码 . 不要复制代码,只需使用帮助程序类并初始化Google SignIn Option和Google API Client . 现在,只要您想使用Google API Client,就可以初始化帮助程序类 .
Case 1. 如果您想在登录前退出,请使用
clearDefaultAccountAndReconnect
.Case 2. 对于签出用户(无论活动是什么),请使用
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback
.这是该项目的github link . 如果你仍然有疑虑,你可以看看 .