使用此类代码可以链接我的应用和使用帐户。
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Plus.API)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addScope(Drive.SCOPE_APPFOLDER)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
mGoogleApiClient.connect();
但是,一旦激活或切换到新帐户,该帐户是否可以“退出”?
答案 0 :(得分:16)
clearDefaultAccount()
无效。要注销并清除所选帐户,请使用Auth.GoogleSignInApi.signOut()
,如下所示:
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Build a GoogleApiClient with access to the Google Sign-In API and the
// options specified by gso.
mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext()) //Use app context to prevent leaks using activity
//.enableAutoManage(this /* FragmentActivity */, connectionFailedListener)
.addApi(Auth.GOOGLE_SIGN_IN_API)
.build();
}
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
private void signOut() {
if (mGoogleApiClient.isConnected()) {
Auth.GoogleSignInApi.signOut(mGoogleApiClient);
mGoogleApiClient.disconnect();
mGoogleApiClient.connect();
}
}
答案 1 :(得分:5)
我找到了这个最新的解决方案:
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
mGoogleApiClient.clearDefaultAccountAndReconnect().setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
mGoogleApiClient.disconnect();
}
});
}
的用法
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
已弃用
https://developers.google.com/android/reference/com/google/android/gms/plus/Account
答案 2 :(得分:0)
修改强>
这种方法已被弃用
像这样使用
if (mGoogleApiClient != null)
{
if (mGoogleApiClient.isConnected())
{
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
mGoogleApiClient.disconnect();
}
}
答案 3 :(得分:0)
我已修改了android文档中的代码段。这是一个可以登录和注销的完整代码段。显示用户名,电子邮件和个人资料图像。此代码段已经过测试。原始文档可在以下位置找到:https://developers.google.com/identity/sign-in/android/sign-in 有关依赖性,请参阅此文档。
public class MainActivity extends AppCompatActivity implements View.OnClickListener, GoogleApiClient.OnConnectionFailedListener {
private ImageView imgUserAvatar;
private TextView lblName;
private TextView lblEmailID;
private SignInButton btnSignIn;
private Button btnSignOut;
private GoogleSignInOptions gso;
private GoogleSignInClient googleSignInClient;
private GoogleSignInAccount account;
private GoogleApiClient googleApiClient;
private static final int RC_SIGN_IN = 58;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
imgUserAvatar = findViewById(R.id.imgUserAvatar);
lblName = findViewById(R.id.lblName);
lblEmailID = findViewById(R.id.lblEmailID);
btnSignIn = findViewById(R.id.btnSignIn);
btnSignOut = findViewById(R.id.btnSignOut);
btnSignIn.setOnClickListener(this);
btnSignOut.setOnClickListener(this);
btnSignIn.setSize(SignInButton.SIZE_STANDARD);
imgUserAvatar.setVisibility(View.GONE);
lblName.setVisibility(View.GONE);
lblEmailID.setVisibility(View.GONE);
btnSignOut.setVisibility(View.GONE);
btnSignIn.setVisibility(View.VISIBLE);
gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
//.requestIdToken(RT_GOOGLE_SIGN_ID_ID)
.requestEmail()
.build();
googleSignInClient = GoogleSignIn.getClient(this, gso);
googleApiClient = new GoogleApiClient.Builder(this).enableAutoManage(this, this).addApi(Auth.GOOGLE_SIGN_IN_API, gso).build();
account = GoogleSignIn.getLastSignedInAccount(this);
updateUI(account);
}
@Override
public void onClick(View view) {
switch(view.getId()) {
case R.id.btnSignIn:
signIn();
break;
case R.id.btnSignOut:
signOut();
break;
}
}
private void signIn() {
Intent signInIntent = googleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
private void signOut() {
Auth.GoogleSignInApi.signOut(googleApiClient).setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(@NonNull Status status) {
imgUserAvatar.setVisibility(View.GONE);
lblName.setVisibility(View.GONE);
lblEmailID.setVisibility(View.GONE);
btnSignOut.setVisibility(View.GONE);
btnSignIn.setVisibility(View.VISIBLE);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
if(requestCode == RC_SIGN_IN) {
//The Task returned from this call is always completed, no need to attach
//a listener.
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
}
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
//Signed in successfully, show authenticated UI.
updateUI(account);
}
catch(ApiException e) {
updateUI(null);
}
}
private void updateUI(final GoogleSignInAccount account) {
if(account != null) {
lblName.setText(account.getDisplayName());
lblEmailID.setText(account.getEmail());
lblName.setVisibility(View.VISIBLE);
lblEmailID.setVisibility(View.VISIBLE);
btnSignOut.setVisibility(View.VISIBLE);
btnSignIn.setVisibility(View.GONE);
final Bitmap[] bitmap = new Bitmap[1];
final Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message message) {
imgUserAvatar.setImageBitmap(bitmap[0]);
imgUserAvatar.setVisibility(View.VISIBLE);
return true;
}
});
new Thread(new Runnable() {
@Override
public void run() {
InputStream in;
try {
in = new java.net.URL(account.getPhotoUrl().toString()).openStream();
bitmap[0] = BitmapFactory.decodeStream(in);
handler.sendEmptyMessage(0);
}
catch(IOException e) {
e.printStackTrace();
}
}
}).start();
}
else if(account == null) {
imgUserAvatar.setVisibility(View.GONE);
lblName.setVisibility(View.GONE);
lblEmailID.setVisibility(View.GONE);
btnSignOut.setVisibility(View.GONE);
btnSignIn.setVisibility(View.VISIBLE);
}//End if(account != null)
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
//ToDo in context only used for signout.
}
}
另外,您可能会发现此视频很有用:https://www.youtube.com/watch?v=2PIaGpJMCNs&t=