Hi
I am creating an Activity(Client)/Service App. The Service is local and bound to the Activity. What I would like to do is send messages from the Service to the Activity. I've looked at the Messenger class and it looks promising, except the only examples I've found have the handler in the Service, where I would like the Handler in the Activity (Client) and am not sure how to reverse the examples I've found. The Client does not need to send messages to the Service. If need be, the Client will make method calls on the Service using the IBinder.
One example I've looked through is there;
https://developer.android.com/guide/components/bound-services
There are many others that do the same thing.
The plan is the Service will open comms with other processes and funnel up messages back to the Client.
Both Client and Service will be a long running process.
Any advice would be appreciated.
Edit:
I have an example of what I'd like to do
The activity example:
Example of the Service:
So as you can see, I'm trying to do two things with my Service, make local method calls on it from the Activity through the IBinder and also have the Service send messages back to the Activity through a Messenger. I have to believe that this is possible, I would like to know if I'm on the right track?
Thanks...
I am creating an Activity(Client)/Service App. The Service is local and bound to the Activity. What I would like to do is send messages from the Service to the Activity. I've looked at the Messenger class and it looks promising, except the only examples I've found have the handler in the Service, where I would like the Handler in the Activity (Client) and am not sure how to reverse the examples I've found. The Client does not need to send messages to the Service. If need be, the Client will make method calls on the Service using the IBinder.
One example I've looked through is there;
https://developer.android.com/guide/components/bound-services
There are many others that do the same thing.
The plan is the Service will open comms with other processes and funnel up messages back to the Client.
Both Client and Service will be a long running process.
Any advice would be appreciated.
Edit:
I have an example of what I'd like to do
The activity example:
Java:
// Activity
public static class Binding extends Activity
{
class IncomingHandler extends Handler /// Handle messages from Service
{
@Override
public void handleMessage(Message msg)
{
switch (msg.what)
{
case MessengerService.MSG_SET_VALUE:
mCallbackText.setText("Received from service: " + msg.arg1); /// MessengerService should be LocalService
break;
default:
super.handleMessage(msg);
}
}
}
final Messenger mMessenger = new Messenger(new IncomingHandler());
private LocalService mBoundService; /// For Local Service
private ServiceConnection mConnection = new ServiceConnection()
{
public void onServiceConnected(ComponentName className, IBinder service)
{
mBoundService = ((LocalService.LocalBinder)service).getService(); /// For Local Service
mService = new Messenger(service);
}
}
void doBindService()
{
bindService(new Intent(Binding.this, MessengerService.class), mConnection, Context.BIND_AUTO_CREATE); /// MessengerService should be LocalService
bindService(new Intent(Binding.this, LocalService.class), mConnection, Context.BIND_AUTO_CREATE)); /// For Local Service
mIsBound = true;
mCallbackText.setText("Binding.");
}
}
Example of the Service:
Java:
// Service
public class LocalService extends Service
{
NotificationManager mNM;
final Messenger mMessenger = new Messenger(new IncomingHandler());
/// The Local Binder
public class LocalBinder extends Binder
{
LocalService getService()
{
return LocalService.this; /// Does this include both the ability to make local methods call and the messenger?
}
}
class IncomingHandler extends Handler
{
@Override
public void handleMessage(Message msg)
{
}
}
private final IBinder mBinder = new LocalBinder(); /// For Local comm with client (method calls)
/// For the Messenger version
@Override
public IBinder onBind(Intent intent)
{
return mMessenger.getBinder();
}
/// For the Local method call version
@Override
public IBinder onBind(Intent intent)
{
return mBinder; /// Does this include the messenger capability?
}
}
So as you can see, I'm trying to do two things with my Service, make local method calls on it from the Activity through the IBinder and also have the Service send messages back to the Activity through a Messenger. I have to believe that this is possible, I would like to know if I'm on the right track?
Thanks...
Last edited: