[英]How to configue hyperlink of Firebase Auth's Dialog
使用 Firebase 身份驗證的 Google 登錄方法時,隱私政策和服務條款的超鏈接不起作用。 請注意,在某些設備上沒有超鏈接。 我關注了文檔: https://firebase.google.com/docs/auth/android/google-signin
源代碼: https://drive.google.com/file/d/1xTy078Wk9ttL5pcgwNv3urSjNHDqqrwN/view?usp=sharing
活動:
public class MainActivity extends AppCompatActivity {
GoogleSignInClient mGoogleSignInClient;
private int RC_SIGN_IN =10996;
private FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createRequest();
// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
findViewById(R.id.loginId).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openSomeActivityForResult();
}
});
}
private void createRequest() {
GoogleSignInOptions gso = new GoogleSignInOptions
.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
}
ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent data = result.getData();
//Do that you need to do with the data
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = task.getResult(ApiException.class);
Log.d("onActivityResult", "firebaseAuthWithGoogle:" + account.getId());
firebaseAuthWithGoogle(account.getIdToken());
}catch (ApiException e) {
// Google Sign In failed, update UI appropriately
Log.d("onActivityResult", "Google sign in failed", e);
}
}
}
});
public void openSomeActivityForResult() {
// Intent intent = new Intent(this, SomeActivity.class);
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
someActivityResultLauncher.launch(signInIntent);
}
@Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
if(currentUser!=null){
startActivity(new Intent(MainActivity.this,SignOut.class));
}
}
private void firebaseAuthWithGoogle(String idToken) {
AuthCredential credential = GoogleAuthProvider.getCredential(idToken, null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d("firebaseAuthWithGoogle", "signInWithCredential:success");
FirebaseUser user = mAuth.getCurrentUser();
if(user!=null){
startActivity(new Intent(MainActivity.this,SignOut.class));
}
} else {
// If sign in fails, display a message to the user.
Log.d("firebaseAuthWithGoogle", "signInWithCredential:failure", task.getException());
}
}
});
}
}
請注意, onActivityResult已棄用。 您應該做的是使用registerForActivityResult 。 在 Java 中,它應該是這樣的:
ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent data = result.getData();
//Do that you need to do with the data
}
}
});
public void openSomeActivityForResult() {
Intent intent = new Intent(this, SomeActivity.class);
someActivityResultLauncher.launch(intent);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.