Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Service-Activity 2 way communication

in my team's Android application I have a service running from boot which communicates with a server to perform operations such as logging in, registering, chatting between phones and updating the phone database.

I need to make my service communicate with the activity bi-directionally: for example I am working on the login activity at the moment and the username and passwords are Strings taken from a text field on the app screen and I have been able to pass them to the service for it to send an authorisation command to the server.

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) {

    }

This works as I would have expected. When the server responds with a message saying whether or not the login was sucessful, I need it to pass a message back to the activity so that I can start the main activity if succesful or prompt for re-entry if not.

I tried to use the msg.replyTo field to get the return messenger to send the information back, but when I run the app it force closes with a null pointer exception and I have no idea why this is happening. Here is the code that seems to be the culprit:

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;

The null pointer seems to be coming from the

msg.replyTo.send(m);

line of code in both cases (login succesful and login failed)

Any help to fix this problem would be greatly appreciated :)

like image 635
peppermcknight Avatar asked Nov 05 '22 04:11

peppermcknight


2 Answers

As Gregg points out in the comments. You need to set msg.replyTo = messenger; int he place where you send the original message.

An example can be found here: http://www.survivingwithandroid.com/2014/01/android-bound-service-ipc-with-messenger.html

like image 60
Patrick Boos Avatar answered Nov 11 '22 12:11

Patrick Boos


I think you forgot to send response to Login Activity by bundle from Service. So, i made some changes in Messenger Service

define one global variable and made some changes in Incoming Handler

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;

Now we have to catch this response in our LoginActivity and take IncomingHandler in Login Activity also

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.

}

This code is working perfectly, hope it will help you, Thanks

like image 41
Ashish Manwal Avatar answered Nov 11 '22 12:11

Ashish Manwal