gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:15:32 24 4
gpt4 key购买 nike

我正在处理“登录” Activity :

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 handler 值吗?

附注我是 Firebase 的新手,正在尝试掌握 Java 中的类的扩展和实现。

最佳答案

我也曾为此苦苦挣扎。我的做法和你想的一样;我创建了一个 AuthHandler 接口(interface):

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

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接口(interface):

public interface ServerAuthenticate {

interface ServerAuthenticateCallback {

void onServerAuthenticated(User user);

void onServerAuthenticateFail(String error);

}

void userSignIn(final User user, ServerAuthenticateCallback callback);

}

要进行身份验证,请在您的 Activity 中使用 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);
}

关于java - 如何将已实现的 Interface 对象传递给其实现的类中的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33572355/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com