1

I implemented a Service and I use a Messenger with its handler as a communication bridge. Every time that I start an activity I have to follow these steps:

1) create the service connection

private ServiceConnection mConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder service) {
        mService = new Messenger(service);
        try {
            Message msg = Message.obtain(null, ConstantStuff.MSG_REGISTER_CLIENT);
            msg.replyTo = mMessenger;
            egoService.send(msg);
        } catch (RemoteException e) {
            Log.e("HELPER", "FAIL - Service crashed" + e.getMessage());
        }
    }

    public void onServiceDisconnected(ComponentName className) {
        mService = null;
    }
};

2) create the messenger

final Messenger mMessenger = new Messenger(new ActivityOne.IncomingMessageHandler());

class IncomingMessageHandler extends Handler {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case ConstantStuff.MACRO_SEND_PWD:
                if (msg.arg1 == 1) {
                    Log.e("PWD", "SUCCESS");
                } else if (msg.arg1 == 0) {
                    Log.e("PWD", "FAIL");
                }
                break;
            default:
                super.handleMessage(msg);
        }
    }
}

3) bind the service to my activity

public void doBindService() {
    this.bindService(new Intent(this, NetworkService.class), mConnection, Context.BIND_AUTO_CREATE);
    isBound = true;
    Log.e("HELPER", "DO BIND");
}

4) start using the service

private void sendPwd(String pwd) {
    if (isBound) {
        if (mService != null) {
            try {
                Bundle b = new Bundle();
                b.putByte("Command", (byte) ConstantStuff.MACRO_SEND_PWD);
                b.putString("Password", password);
                b.putString("Param0", password);
                b.putString("Param1", "dummy");
                b.putString("Param2", "dummy");
                b.putString("Param3", "dummy");
                Message msg = Message.obtain(null, ConstantStuff.MSG_MACRO);
                msg.setData(b);
                msg.replyTo = mMessenger;
                mService.send(msg);
            } catch (Exception e) {}
        }
    }
}

5) at the end of my task unbind the service from my activity

public void doUnbindService() {
    if (isBound) {
        if (mService != null) {
            try {
                Message msg = Message.obtain(null, ConstantStuff.MSG_UNREGISTER_CLIENT);
                msg.replyTo = mMessenger;
                mService.send(msg);
            } catch (RemoteException e) {
                Log.e("HELPER", "FAIL - DO UnBIND" + e.getMessage());
            }
        }
        this.unbindService(mConnection);
        isBound = false;
        Log.e("HELPER", "DO UnBIND");
    }
}

I need to communicate to my service in almost all the activities of my app and I'd like to not repeat all the code every time. Does extend all the activities to a father activity a good solution? Is it possible to write all the communication code in the "Application"?

michoprogrammer
  • 1,159
  • 2
  • 18
  • 45

1 Answers1

1

In whichever Activities you want the service to interact with the UI, use "bindService", its callback methods "onServiceConnected" and "onServiceDisconnected" on those Activities. If you want the service to run even if app is closed, use startService with startsticky/startnonsticky/startforeground. For all the Activities you just need one service class. In your case you have already coded the service class which is perfect, and also an Activity. Now bind in rest of the Activities as mentioned in the beginning. I haven't used Messenger yet, so kindly make sure of that.