5

在我团队的 Android 应用程序中,我有一个从启动运行的服务,它与服务器通信以执行诸如登录、注册、手机之间聊天和更新手机数据库等操作。

我需要让我的服务与活动进行双向通信:例如,我目前正在处理登录活动,用户名和密码是从应用程序屏幕上的文本字段中获取的字符串,我已经能够传递它们到服务,以便它向服务器发送授权命令。

public void loginPressed(View v){
    usernameStr = usernameField.getText().toString();
    passwordStr = passwordField.getText().toString();

    if (!bound) return;
    Bundle b = new Bundle();
    Message msg = Message.obtain(null, ChatService.LOGIN);
    try {
        b.putString("username", usernameStr);
        b.putString("password", passwordStr);
        msg.setData(b);
        messenger.send(msg);
    }
    catch (RemoteException e) {

    }

这如我所料。当服务器回复一条消息说登录是否成功时,我需要它向活动传递一条消息,以便我可以在成功时启动主要活动,如果不成功则提示重新进入。

我尝试使用 msg.replyTo 字段让返回信使将信息发回,但是当我运行应用程序时,它会以空指针异常强制关闭,我不知道为什么会发生这种情况。这是似乎是罪魁祸首的代码:

private class IncomingHandler extends Handler {
    @Override
    public void handleMessage(Message msg) {
        switch(msg.what) {
        case LOGIN:

            Bundle b = msg.getData();
            String username = b.getString("username");
            String password = b.getString("password");

            String loginMessage = TCPCall.login(username, password);
            connection.sendMessage(loginMessage);

            String loginReturn = connection.retrieveMessage();
            Message m;

            Scanner s = new Scanner(loginReturn);
            s.useDelimiter(",");
            String c = s.next();
            String status = s.next();
            String message = s.next();

            if (status.equals("OK")) {
                m = Message.obtain(null, LoginActivity.OK);
                try {
                    msg.replyTo.send(m);
                } catch (RemoteException e) {}
            }
            else {
                m = Message.obtain(null, LoginActivity.ERR);
                try {
                    msg.replyTo.send(m);
                } catch (RemoteException e) {}
            }
            break;

空指针似乎来自

msg.replyTo.send(m);

两种情况下的代码行(登录成功和登录失败)

任何解决此问题的帮助将不胜感激:)

4

2 回答 2

1

我认为您忘记通过服务包发送对登录活动的响应。所以,我在 Messenger 服务中做了一些改变

定义一个全局变量并在传入处理程序中进行了一些更改

static final int LOGIN_STATUS = 1;

private class IncomingHandler extends Handler {
@Override
public void handleMessage(Message msg) {
    switch(msg.what) {
    case LOGIN:

        Bundle b = msg.getData();
        String username = b.getString("username");
        String password = b.getString("password");

        String loginMessage = TCPCall.login(username, password);
        connection.sendMessage(loginMessage);

        String loginReturn = connection.retrieveMessage();
        Message m = Message.obtain(null, LOGIN_STATUS);

        Scanner s = new Scanner(loginReturn);
        s.useDelimiter(",");
        String c = s.next();
        String status = s.next();
        String message = s.next();

        if (status.equals("OK")) {
            b.putString("responseC",c);
            b.putString("responseStatus",status);
            b.putString("responseMessage",message)

            m.setData(b);
            try {
                msg.replyTo.send(m);
            } catch (RemoteException e) {}
        }
        else {
           /*if something is wrong with username and password you can put 
           a toast*/

            }
        break;

现在我们必须在 LoginActivity 中捕获这个响应,并在 Login Activity 中获取 IncomingHandler

class IncomingHandler extends Handler{

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case ChatService.LOGIN_STATUS:
                    String C = msg.getData().getString("responseC");
                    String Status = msg.getData().getString("responseStatus");
                    String Message = msg.getData().getString("responseMessage");

                    //Here is your response in LoginActivity, enjoy!!! 

                    break;

                default:
                    super.handleMessage(msg);
            }
        }
    }

final Messenger mMessenger = new Messenger(new IncomingHandler());

public void loginPressed(View v){
usernameStr = usernameField.getText().toString();
passwordStr = passwordField.getText().toString();

if (!bound) return;
Bundle b = new Bundle();
Message msg = Message.obtain(null, ChatService.LOGIN_SATUS,0,0);
try {
    b.putString("username", usernameStr);
    b.putString("password", passwordStr);
    msg.setData(b);
    msg.replyTo = mMessenger;
    messenger.send(msg);
}
catch (RemoteException e) {
    // In this case the service has crashed before we could even
    // do anything with it; we can count on soon being
    // disconnected (and then reconnected if it can be restarted)
    // so there is no need to do anything here.

}

此代码运行良好,希望对您有所帮助,谢谢

于 2018-09-17T06:18:32.677 回答
1

正如格雷格在评论中指出的那样。您需要msg.replyTo = messenger;在发送原始消息的位置进行设置。

一个例子可以在这里找到:http: //www.survivingwithandroid.com/2014/01/android-bound-service-ipc-with-messenger.html

于 2015-08-12T13:53:52.273 回答