我已经在应用程序上登录了google帐户,但是当我注销系统时,应用程序强制关闭。在另一种情况下,当我再次开始登录该应用程序时无法显示Google帐户选择器
这是我尝试过的代码,问题是方法GoogleSignIn Client上的空对象引用,并且我找不到方法revokeaccess,因为它不可用。
登录Activity.java
package id.co.dolansemarang.loginfirebaseds;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.GoogleAuthProvider;
public class LoginActivity extends BaseActivity implements View.OnClickListener, GoogleApiClient.OnConnectionFailedListener {
private static final String TAG = "Login User";
private static final int RC_SIGN_IN = 1234;
GoogleSignInClient googleSignInClient;
GoogleApiClient mGoogleApiClient;
Button btnLogin;
LinearLayout btnGoogleSignIn;
EditText edtEmailLogin, edtPasswordLogin;
TextView tvResetPass;
FirebaseAuth firebaseAuthLogin;
// DatabaseReference userRefLogin;
FirebaseUser curUser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
btnLogin = findViewById(R.id.btn_login);
edtEmailLogin = findViewById(R.id.edt_email_login);
edtPasswordLogin = findViewById(R.id.edt_password_login);
tvResetPass = findViewById(R.id.tv_reset_pass);
btnGoogleSignIn = findViewById(R.id.btn_sign_in_with_google);
//Google Sign In
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(LoginActivity.this)
.enableAutoManage(LoginActivity.this, this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
googleSignInClient = GoogleSignIn.getClient(this, gso);
firebaseAuthLogin = FirebaseAuth.getInstance();
btnLogin.setOnClickListener(this);
tvResetPass.setOnClickListener(this);
btnGoogleSignIn.setOnClickListener(this);
updateUI(curUser);
}
@Override
protected void onStart() {
super.onStart();
// cek apakah pengguna sudah pernah masuk sehingga ada update UI disini
FirebaseUser currentUser = firebaseAuthLogin.getCurrentUser();
updateUI(currentUser);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == RC_SIGN_IN){
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try{
GoogleSignInAccount account = task.getResult(ApiException.class);
loginWithGoogle(account);
}
catch (ApiException e){
Log.w(TAG, "Google Sign I Failed", e);
updateUI(curUser);
}
}
}
private void loginWithGoogle(GoogleSignInAccount account) {
Log.d(TAG, "FirebaseAuthWithGoogle" +account.getId());
showProgressDialog();
AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(),null);
firebaseAuthLogin.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Log.d(TAG, "your account has been success to register");
FirebaseUser user = firebaseAuthLogin.getCurrentUser();
updateUI(user);
} else {
Log.w(TAG, "please, try again", task.getException());
Toast.makeText(LoginActivity.this, "Gagal Login, silakan coba lagi", Toast.LENGTH_LONG).show();
// updateUI(null);
}
hideProgressDialog();
}
});
}
private void loginUserWithFirebase(String email, String password) {
Log.d(TAG, "signIn:" + email);
if (!validateForm()) {
return;
}
showProgressDialog();
firebaseAuthLogin.signInWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Log.d(TAG, "your account has been success to register");
FirebaseUser user = firebaseAuthLogin.getCurrentUser();
updateUI(user);
} else {
Log.w(TAG, "please, try again", task.getException());
Toast.makeText(LoginActivity.this, "Gagal Login, silakan coba lagi", Toast.LENGTH_LONG).show();
// updateUI(null);
}
hideProgressDialog();
}
});
}
private void updateUI(FirebaseUser user) {
hideProgressDialog();
if (user != null && user.isEmailVerified()) {
startActivity(new Intent(LoginActivity.this, MainActivity.class));
Toast.makeText(this, "Welcome " + user.getEmail() + "", Toast.LENGTH_LONG).show();
finish();
} else if (user != null && !user.isEmailVerified()) {
Toast.makeText(getApplicationContext(), "Please verify your Email, first", Toast.LENGTH_SHORT).show();
} else {
Log.d(TAG, "Selamat datang");
}
}
private boolean validateForm() {
boolean valid = true;
String email = edtEmailLogin.getText().toString();
String password = edtPasswordLogin.getText().toString();
if (TextUtils.isEmpty(email)) {
Toast.makeText(getApplicationContext(), "Harap isi email kembali", Toast.LENGTH_LONG).show();
valid = false;
} else {
if (TextUtils.isEmpty(password)) {
Toast.makeText(getApplicationContext(), "Harap isi password kembali", Toast.LENGTH_LONG).show();
valid = false;
} else {
if (password.length() <= 6) {
Toast.makeText(getApplicationContext(), "password contained minimum 6 character", Toast.LENGTH_LONG).show();
valid = false;
}
}
}
return valid;
}
@Override
public void onClick(View v) {
int i = v.getId();
if (i == R.id.btn_login) {
loginUserWithFirebase(edtEmailLogin.getText().toString(), edtPasswordLogin.getText().toString());
} else if (i == R.id.tv_reset_pass) {
startActivity(new Intent(LoginActivity.this, ResetPasswordActivity.class));
finish();
} else if(i == R.id.btn_sign_in_with_google){
signInGoogle();
}
}
private void signInGoogle() {
Intent signIntent = googleSignInClient.getSignInIntent();
startActivityForResult(signIntent, RC_SIGN_IN);
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
}
Main Activity.java
package id.co.dolansemarang.loginfirebaseds;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
FirebaseAuth firebaseAuthMain;
FirebaseUser user;
Button btnKeluar;
GoogleSignInClient googleSignInClient;
GoogleApiClient googleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnKeluar = findViewById(R.id.btn_sign_out);
firebaseAuthMain = FirebaseAuth.getInstance();
btnKeluar.setOnClickListener(this);
user = firebaseAuthMain.getCurrentUser();
}
@Override
public void onClick(View v) {
int i = v.getId();
if(i == R.id.btn_sign_out){
// firebaseAuthMain.signOut();
// googleSignInClient.signOut().addOnCompleteListener(this, new OnCompleteListener<Void>() {
// @Override
// public void onComplete(@NonNull Task<Void> task) {
// updateUI(null);
// }
// });
// updateUI(user);
signOutApp();
signOutGoogle();
}
}
private void signOutGoogle() {
googleSignInClient.signOut().addOnCompleteListener(this,
new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
updateUI(null);
}
});
}
private void signOutApp() {
firebaseAuthMain.signOut();
updateUI(user);
}
private void updateUI(FirebaseUser user) {
if(user != null){
startActivity(new Intent(MainActivity.this, LoginActivity.class));
Toast.makeText(this,"Thanks for visiting",Toast.LENGTH_LONG).show();
finish();
}
}
}
我希望我先使用电子邮件或Google帐户登录,然后注销而不会发生冲突。那么,我该怎么办?
答案 0 :(得分:0)