我正在尝试创建一种在Firebase上验证Google用户身份的方法。
我在3个不同的活动中使用这个方法,我想清理代码制作方法。
这是我试图在类中作为静态方法的代码。
private void firebaseAuthWithGoogle(GoogleSignInAccount acct, final Context activity, FirebaseAuth firebaseAuth) {
Log.d("MainACtivity", "firebaseAuthWithGoogle:" + acct.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
firebaseAuth.signInWithCredential(credential)
.addOnCompleteListener(activity, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d("Main", "signInWithCredential:onComplete:" + task.isSuccessful());
if (!task.isSuccessful()) {
Log.w("MainAcitivyt", "signInWithCredential", task.getException());
Toast.makeText(activity, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
else{
startActivity(AccountCreationActivity.newIntent(activity));
}
}
});
}
我有问题要理解如何在我的方法调用时使用来自另一个类的'startActivity'。或者我一直在尝试的第二个选择是返回一个意图,但是因为'onComplete'返回void我完全失去了如何继续。
以下是我想要在不同活动中复制的Google注册/登录代码,并计划在另一个类中创建它的方法。我认为我从注册活动中取出的唯一方法就是'onACtivityResult'
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = task.getResult(ApiException.class);
firebaseAuthWithGoogle(account);
} catch (ApiException e) {
// Google Sign In failed, update UI appropriately
Log.w(TAG, "Google sign in failed", e);
// ...
}
}
}
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d("MainACtivity", "firebaseAuthWithGoogle:" + acct.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
firebaseAuth.signInWithCredential(credential)
.addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d("Main", "signInWithCredential:onComplete:" + task.isSuccessful());
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Log.w("MainAcitivyt", "signInWithCredential", task.getException());
Toast.makeText(getActivity(), "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
else{
startActivity(AccountCreationActivity.newIntent(getActivity()));
}
}
});
}
我如何使用另一个类的'startActivity'
使用Context
实例开始活动。你在方法参数中有Context activity
,使用它。
private void firebaseAuthWithGoogle(GoogleSignInAccount acct, final Context activity, FirebaseAuth firebaseAuth) {
Log.d("MainACtivity", "firebaseAuthWithGoogle:" + acct.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
firebaseAuth.signInWithCredential(credential)
.addOnCompleteListener(activity, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d("Main", "signInWithCredential:onComplete:" + task.isSuccessful());
if (!task.isSuccessful()) {
Log.w("MainAcitivyt", "signInWithCredential", task.getException());
Toast.makeText(activity, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
else{
// This line has been changed.
activity.startActivity(AccountCreationActivity.newIntent(activity));
}
}
});
}