从android应用程序中集成的Google Plus注销
我遵循以下教程将Google+集成到android应用程序中。 我能够登录到谷歌帐户,并能够检索信息也。问题是我无法注销 我在登录活动中登录G+,并使用共享首选项存储会话,然后在另一个基本活动中关闭会话,并将布尔值传递给登录活动以关闭会话。尽管会话未处于活动状态或用户尚未登录,但只要登录活动启动,登录活动就会自动连接到G+。试图在onConnected上执行逻辑,但无效 下面是我的代码片段从android应用程序中集成的Google Plus注销,android,google-plus,google-play-services,Android,Google Plus,Google Play Services,我遵循以下教程将Google+集成到android应用程序中。 我能够登录到谷歌帐户,并能够检索信息也。问题是我无法注销 我在登录活动中登录G+,并使用共享首选项存储会话,然后在另一个基本活动中关闭会话,并将布尔值传递给登录活动以关闭会话。尽管会话未处于活动状态或用户尚未登录,但只要登录活动启动,登录活动就会自动连接到G+。试图在onConnected上执行逻辑,但无效 下面是我的代码片段 public class LoginActivity extends BaseActionBar imp
public class LoginActivity extends BaseActionBar implements OnClickListener,
ConnectionCallbacks, OnConnectionFailedListener {
private Button btnLogin, btnFgetPwrd, btnRegister;
// Logcat tag
private static final String TAG = "LoginActivity";
// Google Plus
private static final int GOOGLE_SIGN_IN = 0;
// Google Plus Profile Data
String GpersonName, GpersonPhotoUrl, Gemail, googleError, GCustId;
// Google client to interact with Google API
private GoogleApiClient mGoogleApiClient;
private SignInButton btnGooglePlus;
// A flag indicating that a PendingIntent is in progress and prevents us
// from starting further intents.
private boolean mIntentInProgress;
// Track whether the sign-in button has been clicked so that we know to
// resolve all issues preventing sign-in without waiting.
private boolean mSignInClicked;
private ConnectionResult mConnectionResult;
// Session Manager Class
SessionManager session;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
// Initializing google plus api client
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN).build();
// Session Manager
session = new SessionManager(getApplicationContext());
if (session.isLoggedIn() == false) {
Log.v(TAG, "false");
mSignInClicked = false;
DataStore.LoginGoogle = false;
if (mGoogleApiClient.isConnected()) {
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
mGoogleApiClient.disconnect();
mGoogleApiClient.connect();
}
} else {
Intent i = new Intent(LoginActivity.this, UserProfileActivity.class);
startActivity(i);
}
btnLogin.setOnClickListener(this);
btnFgetPwrd.setOnClickListener(this);
btnRegister.setOnClickListener(this);
btnGooglePlus.setOnClickListener(this);
}
// Facebook and Google Plus
@Override
protected void onActivityResult(int requestCode, int responseCode,
Intent intent) {
if (requestCode == GOOGLE_SIGN_IN) {
if (responseCode != RESULT_OK) {
mSignInClicked = false;
}
mIntentInProgress = false;
if (!mGoogleApiClient.isConnecting()) {
mGoogleApiClient.connect();
}
} else if (requestCode == FB_SIGN_IN) {
Session.getActiveSession().onActivityResult(this, requestCode,
responseCode, intent);
}
}
// Google Plus
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.loginBtn:
// Login Button Clicked
break;
case R.id.loginBtnFrgtPass:
// Forgot Button Clicked
i = new Intent(LoginActivity.this, ForgotPasswordActivity.class);
startActivity(i);
break;
case R.id.loginBtnRegis:
// Register Button Clicked
i = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(i);
break;
case R.id.loginBtn_sign_in:
signInWithGplus();
break;
}
}
// Sign-in into google
private void signInWithGplus() {
if (!mGoogleApiClient.isConnecting()) {
mSignInClicked = true;
resolveSignInError();
}
}
// Method to resolve any sign-in errors
private void resolveSignInError() {
Log.v(TAG, mConnectionResult.toString());
if (mConnectionResult.hasResolution()) {
try {
mIntentInProgress = true;
startIntentSenderForResult(mConnectionResult.getResolution()
.getIntentSender(), GOOGLE_SIGN_IN, null, 0, 0, 0);
// mConnectionResult
// .startResolutionForResult(this, GOOGLE_SIGN_IN);
} catch (SendIntentException e) {
mIntentInProgress = false;
mGoogleApiClient.connect();
}
}
}
// Google+ connection
@Override
public void onConnected(Bundle arg0) {
// TODO Auto-generated method stub
Log.v(TAG, "onConnected");
mSignInClicked = false;
Toast.makeText(this, "User is connected to Google+", Toast.LENGTH_LONG)
.show();
btnLogin.setEnabled(false);
// Get user's information
getProfileInformation();
}
// Google+ connection
@Override
public void onConnectionSuspended(int arg0) {
// TODO Auto-generated method stub
mGoogleApiClient.connect();
}
// Google+ connection
@Override
public void onConnectionFailed(ConnectionResult result) {
// TODO Auto-generated method stub
Log.v(TAG, result.toString());
if (!result.hasResolution()) {
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this,
0).show();
return;
}
if (!mIntentInProgress) {
// Store the ConnectionResult for later usage
mConnectionResult = result;
if (mSignInClicked) {
// The user has already clicked 'sign-in' so we attempt to
// resolve all
// errors until the user is signed in, or they cancel.
resolveSignInError();
}
}
} // Normal Logging in
}
// Google+ connection
@Override
public void onConnected(Bundle arg0) {
// TODO Auto-generated method stub
Log.v(TAG, "onConnected");
if (ShopRunDataStore.LoginGoogle) {
Log.v(TAG, "Google logged in");
mSignInClicked = false;
Toast.makeText(this, "User is connected to Google+",
Toast.LENGTH_LONG).show();
btnfacebook.setEnabled(false);
btnLogin.setEnabled(false);
// Get user's information
getProfileInformation();
} else {
Log.v(TAG, "In if condition to log off");
if (mGoogleApiClient.isConnected()) {
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
mGoogleApiClient.disconnect();
// mGoogleApiClient.connect();
mSignInClicked = false;
btnGooglePlus.setEnabled(true);
btnfacebook.setEnabled(false);
btnLogin.setEnabled(true);
}
}
}
从基本活动注销会话的代码片段
if (session.isLoggedIn()) {
session.logoutUser();
DataStore.LoginGoogle = false;
setOptionTitle(R.id.action_login, "Login");
}
如果您仔细阅读了您提供的链接中的文档,您会发现有一个关于如何注销使用的示例函数
@Override
public void onClick(View view) {
if (view.getId() == R.id.sign_out_button) {
if (mGoogleApiClient.isConnected()) {
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
mGoogleApiClient.disconnect();
mGoogleApiClient.connect();
}
}
}
在“注销用户”下如果您仔细阅读了您提供的链接中的文档,您会看到有一个关于如何注销用户的示例函数
@Override
public void onClick(View view) {
if (view.getId() == R.id.sign_out_button) {
if (mGoogleApiClient.isConnected()) {
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
mGoogleApiClient.disconnect();
mGoogleApiClient.connect();
}
}
}
在“注销用户”下,我找到了解决方案
在参考了以下网站后,我找到了解决方案
我必须在onConnected中检查日志会话,并执行注销过程。
下面是代码片段
public class LoginActivity extends BaseActionBar implements OnClickListener,
ConnectionCallbacks, OnConnectionFailedListener {
private Button btnLogin, btnFgetPwrd, btnRegister;
// Logcat tag
private static final String TAG = "LoginActivity";
// Google Plus
private static final int GOOGLE_SIGN_IN = 0;
// Google Plus Profile Data
String GpersonName, GpersonPhotoUrl, Gemail, googleError, GCustId;
// Google client to interact with Google API
private GoogleApiClient mGoogleApiClient;
private SignInButton btnGooglePlus;
// A flag indicating that a PendingIntent is in progress and prevents us
// from starting further intents.
private boolean mIntentInProgress;
// Track whether the sign-in button has been clicked so that we know to
// resolve all issues preventing sign-in without waiting.
private boolean mSignInClicked;
private ConnectionResult mConnectionResult;
// Session Manager Class
SessionManager session;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
// Initializing google plus api client
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN).build();
// Session Manager
session = new SessionManager(getApplicationContext());
if (session.isLoggedIn() == false) {
Log.v(TAG, "false");
mSignInClicked = false;
DataStore.LoginGoogle = false;
if (mGoogleApiClient.isConnected()) {
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
mGoogleApiClient.disconnect();
mGoogleApiClient.connect();
}
} else {
Intent i = new Intent(LoginActivity.this, UserProfileActivity.class);
startActivity(i);
}
btnLogin.setOnClickListener(this);
btnFgetPwrd.setOnClickListener(this);
btnRegister.setOnClickListener(this);
btnGooglePlus.setOnClickListener(this);
}
// Facebook and Google Plus
@Override
protected void onActivityResult(int requestCode, int responseCode,
Intent intent) {
if (requestCode == GOOGLE_SIGN_IN) {
if (responseCode != RESULT_OK) {
mSignInClicked = false;
}
mIntentInProgress = false;
if (!mGoogleApiClient.isConnecting()) {
mGoogleApiClient.connect();
}
} else if (requestCode == FB_SIGN_IN) {
Session.getActiveSession().onActivityResult(this, requestCode,
responseCode, intent);
}
}
// Google Plus
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.loginBtn:
// Login Button Clicked
break;
case R.id.loginBtnFrgtPass:
// Forgot Button Clicked
i = new Intent(LoginActivity.this, ForgotPasswordActivity.class);
startActivity(i);
break;
case R.id.loginBtnRegis:
// Register Button Clicked
i = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(i);
break;
case R.id.loginBtn_sign_in:
signInWithGplus();
break;
}
}
// Sign-in into google
private void signInWithGplus() {
if (!mGoogleApiClient.isConnecting()) {
mSignInClicked = true;
resolveSignInError();
}
}
// Method to resolve any sign-in errors
private void resolveSignInError() {
Log.v(TAG, mConnectionResult.toString());
if (mConnectionResult.hasResolution()) {
try {
mIntentInProgress = true;
startIntentSenderForResult(mConnectionResult.getResolution()
.getIntentSender(), GOOGLE_SIGN_IN, null, 0, 0, 0);
// mConnectionResult
// .startResolutionForResult(this, GOOGLE_SIGN_IN);
} catch (SendIntentException e) {
mIntentInProgress = false;
mGoogleApiClient.connect();
}
}
}
// Google+ connection
@Override
public void onConnected(Bundle arg0) {
// TODO Auto-generated method stub
Log.v(TAG, "onConnected");
mSignInClicked = false;
Toast.makeText(this, "User is connected to Google+", Toast.LENGTH_LONG)
.show();
btnLogin.setEnabled(false);
// Get user's information
getProfileInformation();
}
// Google+ connection
@Override
public void onConnectionSuspended(int arg0) {
// TODO Auto-generated method stub
mGoogleApiClient.connect();
}
// Google+ connection
@Override
public void onConnectionFailed(ConnectionResult result) {
// TODO Auto-generated method stub
Log.v(TAG, result.toString());
if (!result.hasResolution()) {
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this,
0).show();
return;
}
if (!mIntentInProgress) {
// Store the ConnectionResult for later usage
mConnectionResult = result;
if (mSignInClicked) {
// The user has already clicked 'sign-in' so we attempt to
// resolve all
// errors until the user is signed in, or they cancel.
resolveSignInError();
}
}
} // Normal Logging in
}
// Google+ connection
@Override
public void onConnected(Bundle arg0) {
// TODO Auto-generated method stub
Log.v(TAG, "onConnected");
if (ShopRunDataStore.LoginGoogle) {
Log.v(TAG, "Google logged in");
mSignInClicked = false;
Toast.makeText(this, "User is connected to Google+",
Toast.LENGTH_LONG).show();
btnfacebook.setEnabled(false);
btnLogin.setEnabled(false);
// Get user's information
getProfileInformation();
} else {
Log.v(TAG, "In if condition to log off");
if (mGoogleApiClient.isConnected()) {
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
mGoogleApiClient.disconnect();
// mGoogleApiClient.connect();
mSignInClicked = false;
btnGooglePlus.setEnabled(true);
btnfacebook.setEnabled(false);
btnLogin.setEnabled(true);
}
}
}
我找到了解决办法
在参考了以下网站后,我找到了解决方案
我必须在onConnected中检查日志会话,并执行注销过程。
下面是代码片段
public class LoginActivity extends BaseActionBar implements OnClickListener,
ConnectionCallbacks, OnConnectionFailedListener {
private Button btnLogin, btnFgetPwrd, btnRegister;
// Logcat tag
private static final String TAG = "LoginActivity";
// Google Plus
private static final int GOOGLE_SIGN_IN = 0;
// Google Plus Profile Data
String GpersonName, GpersonPhotoUrl, Gemail, googleError, GCustId;
// Google client to interact with Google API
private GoogleApiClient mGoogleApiClient;
private SignInButton btnGooglePlus;
// A flag indicating that a PendingIntent is in progress and prevents us
// from starting further intents.
private boolean mIntentInProgress;
// Track whether the sign-in button has been clicked so that we know to
// resolve all issues preventing sign-in without waiting.
private boolean mSignInClicked;
private ConnectionResult mConnectionResult;
// Session Manager Class
SessionManager session;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
// Initializing google plus api client
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN).build();
// Session Manager
session = new SessionManager(getApplicationContext());
if (session.isLoggedIn() == false) {
Log.v(TAG, "false");
mSignInClicked = false;
DataStore.LoginGoogle = false;
if (mGoogleApiClient.isConnected()) {
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
mGoogleApiClient.disconnect();
mGoogleApiClient.connect();
}
} else {
Intent i = new Intent(LoginActivity.this, UserProfileActivity.class);
startActivity(i);
}
btnLogin.setOnClickListener(this);
btnFgetPwrd.setOnClickListener(this);
btnRegister.setOnClickListener(this);
btnGooglePlus.setOnClickListener(this);
}
// Facebook and Google Plus
@Override
protected void onActivityResult(int requestCode, int responseCode,
Intent intent) {
if (requestCode == GOOGLE_SIGN_IN) {
if (responseCode != RESULT_OK) {
mSignInClicked = false;
}
mIntentInProgress = false;
if (!mGoogleApiClient.isConnecting()) {
mGoogleApiClient.connect();
}
} else if (requestCode == FB_SIGN_IN) {
Session.getActiveSession().onActivityResult(this, requestCode,
responseCode, intent);
}
}
// Google Plus
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.loginBtn:
// Login Button Clicked
break;
case R.id.loginBtnFrgtPass:
// Forgot Button Clicked
i = new Intent(LoginActivity.this, ForgotPasswordActivity.class);
startActivity(i);
break;
case R.id.loginBtnRegis:
// Register Button Clicked
i = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(i);
break;
case R.id.loginBtn_sign_in:
signInWithGplus();
break;
}
}
// Sign-in into google
private void signInWithGplus() {
if (!mGoogleApiClient.isConnecting()) {
mSignInClicked = true;
resolveSignInError();
}
}
// Method to resolve any sign-in errors
private void resolveSignInError() {
Log.v(TAG, mConnectionResult.toString());
if (mConnectionResult.hasResolution()) {
try {
mIntentInProgress = true;
startIntentSenderForResult(mConnectionResult.getResolution()
.getIntentSender(), GOOGLE_SIGN_IN, null, 0, 0, 0);
// mConnectionResult
// .startResolutionForResult(this, GOOGLE_SIGN_IN);
} catch (SendIntentException e) {
mIntentInProgress = false;
mGoogleApiClient.connect();
}
}
}
// Google+ connection
@Override
public void onConnected(Bundle arg0) {
// TODO Auto-generated method stub
Log.v(TAG, "onConnected");
mSignInClicked = false;
Toast.makeText(this, "User is connected to Google+", Toast.LENGTH_LONG)
.show();
btnLogin.setEnabled(false);
// Get user's information
getProfileInformation();
}
// Google+ connection
@Override
public void onConnectionSuspended(int arg0) {
// TODO Auto-generated method stub
mGoogleApiClient.connect();
}
// Google+ connection
@Override
public void onConnectionFailed(ConnectionResult result) {
// TODO Auto-generated method stub
Log.v(TAG, result.toString());
if (!result.hasResolution()) {
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this,
0).show();
return;
}
if (!mIntentInProgress) {
// Store the ConnectionResult for later usage
mConnectionResult = result;
if (mSignInClicked) {
// The user has already clicked 'sign-in' so we attempt to
// resolve all
// errors until the user is signed in, or they cancel.
resolveSignInError();
}
}
} // Normal Logging in
}
// Google+ connection
@Override
public void onConnected(Bundle arg0) {
// TODO Auto-generated method stub
Log.v(TAG, "onConnected");
if (ShopRunDataStore.LoginGoogle) {
Log.v(TAG, "Google logged in");
mSignInClicked = false;
Toast.makeText(this, "User is connected to Google+",
Toast.LENGTH_LONG).show();
btnfacebook.setEnabled(false);
btnLogin.setEnabled(false);
// Get user's information
getProfileInformation();
} else {
Log.v(TAG, "In if condition to log off");
if (mGoogleApiClient.isConnected()) {
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
mGoogleApiClient.disconnect();
// mGoogleApiClient.connect();
mSignInClicked = false;
btnGooglePlus.setEnabled(true);
btnfacebook.setEnabled(false);
btnLogin.setEnabled(true);
}
}
}
我的代码的问题是重新登录发生在断开连接完成之前。解决方案是仅在断开连接完成后才开始重新登录,即在
OnConnected
侦听器中。我的代码的问题是,重新登录发生在断开连接完成之前。解决方案是仅在断开连接完成后,即在OnConnected
侦听器中开始重新登录。导入com.google.android.gms.auth.api.auth代码>
Auth.GoogleSignInApi.signOut(谷歌客户端)代码>
注销当前已登录用户(如果有)。它还将清除用户先前选择的帐户,将来登录尝试将要求用户再次选择帐户
注意-GoogleAppClient
对象应处于已连接状态才能注销用户。导入com.google.android.gms.auth.api.auth代码>
Auth.GoogleSignInApi.signOut(谷歌客户端)代码>
注销当前已登录用户(如果有)。它还将清除用户先前选择的帐户,将来登录尝试将要求用户再次选择帐户
注意-GoogleAppClient
对象应处于已连接状态才能注销用户。如果您正确阅读了我的代码,在检查我的会话后已经编写了该函数,但在断开连接后,我仍然无法从代码示例中的G+why注销,我们重新连接!!!mGoogleApiClient.connect();这是来自Google+SDK的示例代码,所以我猜他们会这么做,这样用户就可以使用不同的帐户登录。如果您只是想注销用户,而不是提示他使用其他帐户登录,则应删除该选项。如果您正确阅读了我的代码,并且在检查我的会话后已经编写了该函数,但在断开连接后,我仍然无法从代码示例中的G+why中注销,我们重新连接!!!mGoogleApiClient.connect();这是来自Google+SDK的示例代码,所以我猜他们会这么做,这样用户就可以使用不同的帐户登录。如果您只是想让用户注销,而不是提示他使用其他帐户登录,则应删除该选项。面临相同的问题。ShopRunDataStore
?这是我的数据存储文件。ShopRunDataStore
?这是我的数据存储文件。