i am new in android app and i am doing google+ integration with my android app but i am facing problem in logout from google account .i have seen many tutorials and also download many demo projects but all in vain. also the login flow is not up to the mark. below i am going to explain u the steps performed by the app after login button clicked. sorry i can't post snapshots of my app screens.
here below is my code:
MainActivity.java
package com.gem.google;
import java.util.Arrays;
import java.util.List;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import com.gem.google.R;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.plus.Plus;
public class MainActivity extends FragmentActivity implements
ConnectionCallbacks , OnConnectionFailedListener
{
/* Request code used to invoke sign in user interactions. */
private static final int RC_SIGN_IN = 0;
private boolean mSignInClicked;
/* Store the connection result from onConnectionFailed callbacks so that we can
* resolve them when the user clicks sign-in.
*/
private ConnectionResult mConnectionResult;
/* Client used to interact with Google APIs. */
private GoogleApiClient mGoogleApiClient;
private boolean mIntentInProgress;
private SignInButton signinbt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGoogleApiClient = build_GoogleApiClient();
setContentView(R.layout.activity_main);
signinbt=(SignInButton) findViewById(R.id.sign_in_button);
signinbt.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (view.getId() == R.id.sign_in_button
&& !mGoogleApiClient.isConnecting()) {
mSignInClicked = true;
resolveSignInError();
}
}});
}
private GoogleApiClient build_GoogleApiClient() {
// When we build the GoogleApiClient we specify where connected and
// connection failed callbacks should be returned, which Google APIs our
// app uses and which OAuth 2.0 scopes our app requests.
return new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN)
.build();
}
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RC_SIGN_IN) {
if (resultCode != RESULT_OK) {
mSignInClicked = false;
}
mIntentInProgress = false;
if (!mGoogleApiClient.isConnecting()) {
mGoogleApiClient.connect();
}
}
}
private void resolveSignInError() {
if (mConnectionResult.hasResolution()) {
try {
mIntentInProgress = true;
startIntentSenderForResult(mConnectionResult.getResolution().getIntentSender(),
RC_SIGN_IN, null, 0, 0, 0);
} catch (SendIntentException e) {
// The intent was canceled before it was sent. Return to the default
// state and attempt to connect to get an updated ConnectionResult.
mIntentInProgress = false;
mGoogleApiClient.connect();
}
}
}
public void onConnectionFailed(ConnectionResult result) {
if (!mIntentInProgress) {
// Store the ConnectionResult so that we can use it later when the user clicks
// 'sign-in'.
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();
}
}
}
public void onConnected(Bundle connectionHint) {
mSignInClicked = false;
Intent i = new Intent(MainActivity.this,nextscreen.class);
startActivity(i);
}
public void onConnectionSuspended(int cause) {
mGoogleApiClient.connect();
}
public void onDisconnected() {
// TODO Auto-generated method stub
}
}
nextscren.java
import android.widget.Button;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.plus.Plus;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
//import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
//import android.widget.Button;
public class nextscreen extends Activity implements
GoogleApiClient.ConnectionCallbacks {
private Button signoutbt;
private GoogleApiClient mGoogleApiClient;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
// .addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN)
.build();
setContentView(R.layout.screen_next);
signoutbt=(Button) findViewById(R.id.btn_sign_out);
signoutbt.setVisibility (View.GONE);
if(mGoogleApiClient.isConnected())
{
signoutbt.setVisibility (View.VISIBLE);
signoutbt.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (view.getId() == R.id.btn_sign_out) {
if (mGoogleApiClient.isConnected()) {
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
mGoogleApiClient.disconnect();
mGoogleApiClient.connect();
// CookieSyncManager.createInstance(mGoogleApiClient);
// CookieManager cookieManager = CookieManager.getInstance();
// cookieManager.removeAllCookie();
//CookieSyncManager.getInstance().sync() ;
signoutbt.setVisibility (View.GONE);
Intent i = new Intent(nextscreen.this,MainActivity.class);
startActivity(i);
// Toast.makeText(this, "User is logout!", Toast.LENGTH_LONG).show();
}
}
}
});
}
}
public void onConnected(Bundle connectionHint) {
}
public void onConnectionSuspended(int cause) {
mGoogleApiClient.connect();
}
}
The nextscreen activity is a class where the the control goes on connected after login and where i write the code for logout button.
so what i want is :
step 7 at above should not be there ie before consent screen login screen does not appear.
so please guys help me and tell me where i am wrong. and sorry for this long question but i am helpless and have no choice. please understand and suggest something.
Am glad to have finally resolved this issue after a long time of researching. Bellow is the method for SigningOut
private void signOutFromGplus() {
if (mGoogleApiClient.isConnected()) {
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
mGoogleApiClient.disconnect();
}
}
After which at onPause of the fragment or activity, call the signOutFromGplus since all the details i need are already on my SharedPreference which i use all through my app.
@Override
public void onPause() {
super.onPause();
signOutFromGplus();
}
So on coming back to the fragment, its already SignedOut. So it wont automatically Login anymore.
Hope this helps.``
Regards
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.