我使用Google SignInButton对Google进行了Firebase身份验证。单击按钮时,它会显示您在手机中添加的所有Google帐户。
我正在寻找在您选择帐户时获取电子邮件。
我暂时不需要登录(在代码中注释),只需显示电子邮件 吐司或其他东西。
帐户选择的图片:
我的代码:
public class LoginActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener {
private GoogleApiClient googleApiClient;
private SignInButton signInButton;
private Button buttonCheck;
public static final int SIGN_IN_CODE = 777;
private FirebaseAuth firebaseAuth;
private FirebaseAuth.AuthStateListener firebaseAuthListener;
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
googleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
buttonCheck = (Button) findViewById(R.id.check);
signInButton = (SignInButton) findViewById(R.id.entrar);
signInButton.setSize(SignInButton.SIZE_WIDE);
signInButton.setColorScheme(SignInButton.COLOR_DARK);
signInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = Auth.GoogleSignInApi.getSignInIntent(googleApiClient);
startActivityForResult(intent, SIGN_IN_CODE);
}
});
buttonCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkEmail();
}
});
firebaseAuth = FirebaseAuth.getInstance();
firebaseAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
goMainScreen();
}
}
};
progressBar = (ProgressBar) findViewById(R.id.progressBar);
}
@Override
protected void onStart() {
super.onStart();
firebaseAuth.addAuthStateListener(firebaseAuthListener);
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
public void checkEmail(){
firebaseAuth.fetchSignInMethodsForEmail("mytestemail@gmail.com").addOnCompleteListener(new OnCompleteListener<SignInMethodQueryResult>() {
@Override
public void onComplete(@NonNull Task<SignInMethodQueryResult> task) {
boolean Existeix = !task.getResult().getSignInMethods().isEmpty();
if(Existeix){
Toast.makeText(LoginActivity.this, "EXIST", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(LoginActivity.this, "NO EXIST", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SIGN_IN_CODE) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
private void handleSignInResult(GoogleSignInResult result) {
if (result.isSuccess()) {
firebaseAuthWithGoogle(result.getSignInAccount());
} else {
Toast.makeText(this, "NO S'HA POGUT CONNECTAR", Toast.LENGTH_SHORT).show();
}
}
private void firebaseAuthWithGoogle(final GoogleSignInAccount signInAccount) {
//progressBar.setVisibility(View.VISIBLE);
//signInButton.setVisibility(View.GONE);
AuthCredential credential = GoogleAuthProvider.getCredential(signInAccount.getIdToken(), null);
Toast.makeText(this, "THIS WILL TOAST THE EMAIL", Toast.LENGTH_SHORT).show();
/*firebaseAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
progressBar.setVisibility(View.GONE);
signInButton.setVisibility(View.VISIBLE);
if (!task.isSuccessful()) {
Toast.makeText(getApplicationContext(), "NO S'HA POGUT AUTENTIFICAR", Toast.LENGTH_SHORT).show();
}
}
});*/
}
private void goMainScreen() {
Intent intent = new Intent(this, MainMenu.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
@Override
protected void onStop() {
super.onStop();
if (firebaseAuthListener != null) {
firebaseAuth.removeAuthStateListener(firebaseAuthListener);
}
}
}
答案 0 :(得分:1)
要收到电子邮件,请执行此操作
FirebaseUser user = firebaseAuth.getCurrentUser();
然后检索这样的电子邮件并显示&#34; email&#34;进入你的祝酒词
String email = user.getEmail();
您也可以查看用户的电子邮件是否已经过验证
boolean emailVerified = user.isEmailVerified();
希望它有所帮助,快乐编码!
答案 1 :(得分:1)
一小时后我找到了解决方案:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SIGN_IN_CODE) {
final GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
GoogleSignInAccount acct = result.getSignInAccount();
email = acct.getEmail();
Toast.makeText(LoginActivity.this, email, Toast.LENGTH_SHORT).show();
firebaseAuth.fetchSignInMethodsForEmail(email).addOnCompleteListener(new OnCompleteListener<SignInMethodQueryResult>() {
@Override
public void onComplete(@NonNull Task<SignInMethodQueryResult> task) {
exist = !task.getResult().getSignInMethods().isEmpty();
if(exist){
Toast.makeText(LoginActivity.this, "EXIST",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(LoginActivity.this, "NO EXIST", Toast.LENGTH_SHORT).show();
}
}
});
}
}