【问题标题】:How do I pass an implemented Interface object to a method in the class its implemented in?如何将实现的接口对象传递给其实现的类中的方法?
【发布时间】:2015-11-06 17:29:10
【问题描述】:

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

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

现在我想从AsynTask&lt;Void, Void, Boolean&gt;.doInBackground(Void...params) 中调用Firebase.authWithPassword(String, String, Firebase.AuthResultHandler);

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

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

【问题讨论】:

    标签: java android interface firebase


    【解决方案1】:

    我也为此苦苦挣扎。我这样做的方式就像你在想的那样;我创建了一个 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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-04
      • 2017-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多