I am trying to get two-way communication between my Service and Activity set up. To do this, I start my Service like normal, send a Message with the replyTo set to the Messenger in my Activity, and send it to my Service. That way I have a reference to send messages in both directions. Here is some relevant code in my Activty:
private Messenger mService = null;
private final Messenger mMessenger = new Messenger(new IncomingHandler());
private ServiceConnection mConnection = new ServiceConnection()
{
public void onServiceConnected(ComponentName className, IBinder service)
{
mService = new Messenger(service);
mBound = true;
}
public void onServiceDisconnected(ComponentName className)
{
mService = null;
mBound = false;
}
};
private void initMessenger()
{
Message messageHandler = Message.obtain(null, NetworkService.MSG_GET_HANDLER, 0, 0);
messageHandler.replyTo = mMessenger; //Set the replyTo to give a reference
try
{
mService.send(messageHandler);
}
catch(RemoteException e)
{
e.printStackTrade();
}
catch(NullPointerException e)
{
e.printStackTrace(); //Why!!!
}
}
public void onCreate()
{
...
//Create the intent to start the network service
Intent iService = new Intent(this, NetworkService.class);
int[] connectionConfig = {CONNECTION_BT};
iService.putExtra(CONNECTION_CONFIG, connectionConfig);
bindService(iService, mConnection, Context.BIND_ABOVE_CLIENT);
startService(iService);
initMessenger();
}
A NullPointerException is thrown when I try to call mService.send(), most likely because mService is null at the time. If I move the initMessenger() code inside a Button’s OnClickListener it works fine which is where the confusion comes in. Am I supposed to wait a certain amount of time after starting a Service to use it?
mServicebeingnullat this stage suggests that by this time your Service has not (yet) calledonServiceConnected().Unfortunately the documentation doesn’t suggest there would be a gap between
bindService()returning and the method in theServiceConnectionbeing called.Infact,
bindService()should returntruewhen the Service has been started meaning once the code passes this stage the Service should either be started or not. Therefore I would check if bindService() has returnedtrueor not before callinginitMessenger()as it’s possible the Service has failed to start correctly.It’s also possible that the code within
onServiceConnected()is throwing an exception (which, since it is possible it’s being called on a worker thread, could not cause your app to crash), you may want to check back in your log’s to check if this is the case.Also,
startService(iService);is redundant in this context,bindService()starts the Service, callingstartServiceon the service simply send’s it a message.