You should use and Handlers... Not so easy to implement.
//ON SERVICE SIDE
public class YourService extends Service {
public static final int SAY_HELLO = 1;
public static final int REPLY_HELLO = 2;
private int cnt = 0;
class IncomingHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case SAY_HELLO:
msg.replyTo.send(Message.obtain(null, REPLY_HELLO, cnt++, 0));
break;
}
}
/**
* Target we publish for clients to send messages to IncomingHandler.
*/
final IncomingHandler mHandler = new IncomingHandler();
final Messenger mMessenger = new Messenger(mHandler);
}
//ON CLIENT SIDE
...
final Messenger mMessenger = new Messenger(new IncomingHandler()); //You can do it...
private Messenger mBoundService;
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
mBoundService = new Messenger(service);
mIsBound = true;
try{
Message msg = Message.obtain(null, 1);
msg.replyTo = mMessenger;
mBoundService.send(msg);
} catch (RemoteException ex){
ex.printStackTrace();
}
}
public void onServiceDisconnected(ComponentName className) {
mBoundService = null;
Toast.makeText(context, "Connection GONE", Toast.LENGTH\_SHORT).show();
}
};
public void doBindService() {
Intent intent = new Intent("com.yourpackage.YourService");
if (!context.bindService(intent, mConnection, Context.BIND\_AUTO\_CREATE)) {
try {
context.unbindService(mConnection);
} catch (Throwable t) {}
// Clean up
return;
}
}
public void doUnbindService() {
if (mIsBound) {
Message msg = Message.obtain(null, MESSAGE\_UNREGISTER\_CLIENT);
msg.replyTo = mMessenger;
try{
mBoundService.send(msg);
} catch (RemoteException ex){
}
context.unbindService(mConnection);
mIsBound = false;
}
}
So Sry... Im not so good helping ppl. Just wanted to share this, becouse it has been hard for me to.