1

我正在制作一个 android 库项目来使用我们的 API。遵循 MVC 规则,我创建了一些代理来集中某些任务的使用。例如,我有一个 LoginProxy,它处理所有涉及登录的请求、数据……。要达到这个目的,您必须调用 LoginProxy.getInstance().methodname。当你想登录时,你必须调用代理上的登录方法,它执行一个AsyncTask,执行一个对服务器的webservice调用。当调用的答案返回到 LoginProxy 时,LoginProxy 必须调度一个事件,通知组件(活动)有关更改。此刻,我已经做出了自己的事件调度机制。使用此 LoginProxy 的活动必须将 EventListener 添加到 LoginProxy 的事件中,以便收到有关更改的通知。我添加了我的 LoginProxy 代码。

public class LoginProxy extends EventDispatcher implements LoginUserListener {

    private boolean loggedIn;
    private boolean loggingIn;
    private String userName;
    private UserConfiguration userConfiguration;
    private Credentials credentials; 

    private LoginProxy(){}

    /**
     * Gets a singleton instance of this class
     */
    private static LoginProxy _instance;
    public static LoginProxy getInstance(){
        if(_instance == null) {
            _instance = new LoginProxy();
        }
        return _instance;
    }

    public boolean isLoggedIn() {
        return loggedIn;
    }
    private void setLoggedIn(boolean loggedIn) {
        this.loggedIn = loggedIn;
        dispatchEvent(new GeneralEvent(GeneralEventType.LOGGED_IN_CHANGED));
    }

    public boolean isLoggingIn() {
        return loggingIn;
    }
    private void setLoggingIn(boolean loggingIn) {
        this.loggingIn = loggingIn;
        dispatchEvent(new GeneralEvent(GeneralEventType.LOGGING_IN_CHANGED));
    }

    public String getUserName() {
        return userName;
    }
    private void setUserName(String userName) {
        this.userName = userName;
    }

    public UserConfiguration getUserConfiguration() {
        return userConfiguration;
    }
    private void setUserConfiguration(UserConfiguration userConfiguration) {
        this.userConfiguration = userConfiguration;
    }

    public Credentials getCredentials() {
        return credentials;
    }
    private void setCredentials(Credentials credentials) {
        this.credentials = credentials;
    }

    public void login(String userName, String userPassword, String applicationId, String applicationPassword, String clientVersion){
        setLoggingIn(true);
        LoginUserLauncher launcher = new LoginUserLauncher(this);
        launcher.loginUser(userName, userPassword, applicationId, applicationPassword, clientVersion);
        setUserName(userName);
    }

    public void logout(){
        setUserName(null);
        setUserConfiguration(null);
        setCredentials(null);
        setLoggedIn(false);
    }

    @Override
    public void onLoginUserDone(LoginUserLauncher sender, StatusCode responseStatusCode, UserConfiguration userConfiguration, String sessionCode) {
        setLoggingIn(false);

        if(responseStatusCode == StatusCode.OK){
            setLoggedIn(true);
            setUserConfiguration(userConfiguration);
            Credentials c = new Credentials(userConfiguration.getUserID(), userConfiguration.getUserPassword(), sender.getApplicationId(), sender.getApplicationPassword(), sessionCode);
            setCredentials(c);

            dispatchEvent(new LoginEvent(LoginEventType.LOGIN_SUCCESS, responseStatusCode, this.userConfiguration));
        }else{
            setUserName(null);
            setUserConfiguration(null);
            setCredentials(null);
            setLoggedIn(false);

            dispatchEvent(new LoginEvent(LoginEventType.LOGIN_FAILED, responseStatusCode, this.userConfiguration));
        }
    }
}

我相信这很好用,但现在我意识到也许也可以将 Android 服务用于这些代理。然后我可以发送广播而不是自制事件。我现在的问题是:可以为这些代理使用服务吗?如果是这样,我想我必须制作一个用于绑定的aidl接口?

谁能帮我解决这个问题?谢谢

4

0 回答 0