Android 如何清除google id的会话和令牌,以便再次登录
在我的应用程序中,我可以登录到google+并获取数据,但当我单击注销,然后再次单击登录时,它会显示prevoius登录id的数据,但我希望在注销后,再次登录时根据所有登录要求询问登录id和密码。请提出一些建议 main活动Android 如何清除google id的会话和令牌,以便再次登录,android,google-play-services,Android,Google Play Services,在我的应用程序中,我可以登录到google+并获取数据,但当我单击注销,然后再次单击登录时,它会显示prevoius登录id的数据,但我希望在注销后,再次登录时根据所有登录要求询问登录id和密码。请提出一些建议 main活动 public class MainActivity extends Activity implements OnClickListener, ConnectionCallbacks, OnConnectionFailedListener { pri
public class MainActivity extends Activity implements OnClickListener,
ConnectionCallbacks, OnConnectionFailedListener {
private static final int RC_SIGN_IN = 0;
private static final String TAG = "MainActivity";
private static final int PROFILE_PIC_SIZE = 400;
private GoogleApiClient mGoogleApiClient;
private boolean mIntentInProgress;
private boolean mSignInClicked;
private ConnectionResult mConnectionResult;
private SignInButton btnSignIn;
private Button btnSignOut, btnRevokeAccess,post;
private ImageView imgProfilePic;
private TextView txtName, txtEmail;
private LinearLayout llProfileLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
btnSignIn = (SignInButton) findViewById(R.id.signin);
btnSignOut = (Button) findViewById(R.id.signout);
post = (Button) findViewById(R.id.postToWall);
btnRevokeAccess = (Button) findViewById(R.id.revoke);
imgProfilePic = (ImageView) findViewById(R.id.imgProfilePic);
txtName = (TextView) findViewById(R.id.txtName);
txtEmail = (TextView) findViewById(R.id.txtEmail);
llProfileLayout = (LinearLayout) findViewById(R.id.llProfile);
btnSignIn.setOnClickListener(this);
btnSignOut.setOnClickListener(this);
btnRevokeAccess.setOnClickListener(this);
post.setOnClickListener(this);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).addApi(Plus.API, null)
.addScope(Plus.SCOPE_PLUS_LOGIN).build();
}
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
private void resolveSignInError() {
if (mConnectionResult.hasResolution()) {
try {
mIntentInProgress = true;
mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
} catch (SendIntentException e) {
mIntentInProgress = false;
mGoogleApiClient.connect();
}
}
}
@Override
public void onConnectionFailed(ConnectionResult result) {
if (!result.hasResolution()) {
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this,
0).show();
return;
}
if (!mIntentInProgress) {
mConnectionResult = result;
if (mSignInClicked) {
resolveSignInError();
}
}
}
@Override
protected void onActivityResult(int requestCode, int responseCode,
Intent intent) {
if (requestCode == RC_SIGN_IN) {
if (responseCode != RESULT_OK) {
mSignInClicked = false;
}
mIntentInProgress = false;
if (!mGoogleApiClient.isConnecting()) {
mGoogleApiClient.connect();
}
}
}
@Override
public void onConnected(Bundle arg0) {
mSignInClicked = false;
Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();
getProfileInformation();
updateUI(true);
}
private void updateUI(boolean isSignedIn) {
if (isSignedIn) {
btnSignIn.setVisibility(View.GONE);
btnSignOut.setVisibility(View.VISIBLE);
btnRevokeAccess.setVisibility(View.VISIBLE);
llProfileLayout.setVisibility(View.VISIBLE);
post.setVisibility(View.VISIBLE);
} else {
btnSignIn.setVisibility(View.VISIBLE);
btnSignOut.setVisibility(View.GONE);
btnRevokeAccess.setVisibility(View.GONE);
llProfileLayout.setVisibility(View.GONE);
post.setVisibility(View.GONE);
}
}
@Override
public void onConnectionSuspended(int arg0) {
mGoogleApiClient.connect();
updateUI(false);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.signin:
signInWithGplus();
break;
case R.id.signout:
signOutFromGplus();
break;
case R.id.postToWall:
shareOnGooglePlus(this, "caption");
break;
case R.id.revoke:
revokeGplusAccess();
break;
}
}
private void signInWithGplus() {
if (!mGoogleApiClient.isConnecting()) {
mSignInClicked = true;
resolveSignInError();
}
}
private void signOutFromGplus() {
if (mGoogleApiClient.isConnected()) {
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
mGoogleApiClient.disconnect();
mGoogleApiClient.connect();
updateUI(false);
}
}
你需要撤销谷歌的访问权限,在你的注销方法中添加下面的代码。而不是在onResult中连接到GoogleAppClient,在您的代码中,您只是在断开与GoogleAPI客户端的连接
Plus.AccountApi.revokeAccessAndDisconnect(googleApiClient)
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status arg0) {
Log.e(TAG, "User access revoked!");
mGoogleApiClient.connect();
}
});
Plus.AccountApi.revokeAccessAndDisconnect(GoogleAppClient)
.setResultCallback(新的ResultCallback(){
@凌驾
公共void onResult(状态arg0){
Log.e(标记“用户访问权限已撤销!”);
mGoogleApiClient.connect();
}
});
请在此完成代码:
private void signOutFromGplus() {
if (mGoogleApiClient.isConnected()) {
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
Plus.AccountApi.revokeAccessAndDisconnect(googleApiClient)
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status arg0) {
Log.e(TAG, "User access revoked!");
mGoogleApiClient.connect();
updateUI(false);
}
});
}
}
private void signOutFromGplus(){
if(mgoogleapClient.isConnected()){
Plus.AccountApi.clearDefaultAccount(mgoogleapClient);
Plus.AccountApi.revokeAccessAndDisconnect(GoogleAppClient)
.setResultCallback(新的ResultCallback(){
@凌驾
公共void onResult(状态arg0){
Log.e(标记“用户访问权限已撤销!”);
mGoogleApiClient.connect();
更新(假);
}
});
}
}