Bom Dia,
Controller
MainActivity
HomeActivity
-- Estou fazendo uma aplicação que faz login com o google, facebook e twitter. Facebook e twitter funcionam perfeitamente. Porém com o google eu não consigo fazer o logout. Isso acontece quando eu tento colocar o método de logout em uma classe utilitária, ou seja, se está na mesma classe do login, o logout funciona perfeitamente.
A mensagem que eu recebo é essa:
java.lang.IllegalStateException: GoogleApiClient is not connected yet.
Util
public class Util {
public static void logout() {
if(Controller.mGoogleApiClient.isConnected()){
Auth.GoogleSignInApi.signOut(Controller.mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
pd.dismiss();
Log.d(Constants.TAG, "deslogou");
}
});
}
}
}
Controller
public class Controller {
private static Controller instance = null;
public static GoogleApiClient mGoogleApiClient;
private Controller(){
}
public static Controller getInstance(){
if(instance == null){
instance = new Controller();
}
return instance;
}
}
MainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// iniciar google
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
if (Controller.mGoogleApiClient == null){
Controller.mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
}
setContentView(R.layout.activity_main);
googleButton = (SignInButton) findViewById(R.id.sign_in_button);
googleButton.setSize(SignInButton.SIZE_STANDARD);
googleButton.setScopes(gso.getScopeArray());
googleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
signIn();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Constants.RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(Controller.mGoogleApiClient);
startActivityForResult(signInIntent, Constants.RC_SIGN_IN);
}
private void handleSignInResult(GoogleSignInResult result) {
Log.d("Teste", "handleSignInResult:" + result.isSuccess());
pd = new ProgressDialog(MainActivity.this);
pd.setMessage("Carregando...");
pd.setCancelable(false);
pd.show();
if (result.isSuccess()) {
GoogleSignInAccount google = result.getSignInAccount();
Util.trocarActivity(MainActivity.this, HomeActivity.class);
pd.dismiss();
}
}
@Override
protected void onStart() {
super.onStart();
Controller.mGoogleApiClient.connect();
}
HomeActivity
logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Util.logout();
}
};
You received this message because you are subscribed to the Google Groups "Android Brasil - Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to androidbrasil-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
0 comentários:
Postar um comentário