2015-11-06 36 views
1

我與一個「登錄」活動的工作:如何將實現的接口對象傳遞給其實現的類中的方法?

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

現在,我想打電話給Firebase.authWithPassword(String, String, Firebase.AuthResultHandler);AsynTask<Void, Void, Boolean>.doInBackground(Void...params)內。

我該如何通過Firebase.AuthResultHandlerFirebase.authWithPassword(x,y,a);而不需要創建auth處理程序?可能嗎?可以引用auth處理程序的值?

P.S.我是Firebase的新手,並試圖掌握在java中擴展和實現類。

回答

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); 
} 
相關問題