如何将已实现的Interface对象传递给其实现的类中的方法?

时间:2015-11-06 17:29:11

标签: java android interface firebase

我正在使用“登录”活动:

LogInActivty extends AppCompatActivity
 implements LoaderCallbacks<Cursor>, Firebase.AuthResultHandler, Firebase.AuthStateListener

现在,我想在Firebase.authWithPassword(String, String, Firebase.AuthResultHandler);内致电AsynTask<Void, Void, Boolean>.doInBackground(Void...params)

如何在不需要创建auth处理程序的情况下将Firebase.AuthResultHandler传递给Firebase.authWithPassword(x,y,a);?可能吗?可以引用auth处理程序值吗?

P.S。我是Firebase的新手,并试图在java中掌握扩展和实现类。

1 个答案:

答案 0 :(得分:0)

我也在努力解决这个问题。我这样做的方式就像你在想;我创建了一个AuthHandler接口:

注意:我创建了一个我传递的用户对象

public class FirebaseServerAuthenticate implements ServerAuthenticate, Firebase.AuthResultHandler {

    public static final String TAG = FirebaseServerAuthenticate.class.getSimpleName();

    private ServerAuthenticateCallback callback;
    private User user;

    @Override
    public void userSignIn(User user, ServerAuthenticateCallback callback) {
        Log.d(TAG, "Firebase authentication starting: " + user);
        this.user = user;
        this.callback = callback;

        // I check for id and token here. you may or may not need this. 
        if(isEmpty(user.getAuthToken()) || isEmpty(user.getUid()))
            callback.onServerAuthenticateFail("User provided no auth token");
        else
            FireHelper.getRoot().authWithOAuthToken(AUTH_TOKEN_TYPE_GOOGLE, user.getAuthToken(), this);
    }

    @Override
    public void onAuthenticated(AuthData authData) {
        Log.d(TAG, "Firebase authentication: successful");
        callback.onServerAuthenticated(user);
    }

    @Override
    public void onAuthenticationError(FirebaseError firebaseError) {
        Log.e(TAG, "Firebase authentication: failed");
        callback.onServerAuthenticateFail(firebaseError.getMessage());
    }

}

然后你需要ServerAuthenticate界面:

public interface ServerAuthenticate {

    interface ServerAuthenticateCallback { 

        void onServerAuthenticated(User user);

        void onServerAuthenticateFail(String error);

    }

    void userSignIn(final User user, ServerAuthenticateCallback callback);

}

要进行身份验证,请在您的活动中使用FirebaseServerAuthenticate.userSignIn

FirebaseServerAuthenticate serverAuthenticate = new FirebaseServerAuthenticate();

public void authenticateUser(User user){
    new AsyncTask<User, Void, Void>() {
            @Override
            protected Void doInBackground(User... params) {
                User user = params[0];
                // I did some more stuff here. Got my auth token from google etc.
                serverAuthenticate.userSignIn(user, new ServerAuthenticate.ServerAuthenticateCallback() {
                   @Override
                   public void onServerAuthenticated(User user) {
                       // Here you are authenticated! 
                       // I kill this activity and open in to my mainActivity
                       finishLogin(user);
                   }

                   @Override
                   public void onServerAuthenticateFail(String error) {
                       // Handle the error. For debug, I just show an alert dialog
                       onError(error);
                   }
               });
                return null;
            }

        }.execute(user);
}