封装在活动之外启动活动的方法

时间:2019-03-17 10:50:32

标签: android authentication android-intent google-authentication

我正在尝试创建一种在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()));
                        }
                    }
                });
    }

1 个答案:

答案 0 :(得分:1)

  

如何使用另一个类中的“ 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));
                        }
                    }
                });
    }