Message objects. This
Handler is the basis for a
Messenger that can then share an
IBinder with the client, allowing the client to send commands to the service using
Message objects. Additionally, the client can define a
Messenger of its own so the service can send messages back.
This is the simplest way to perform interprocess communication (IPC), because the Messenger queues all requests into a single thread so that you don't have to design your service to be thread-safe.
?
If you need your service to communicate with remote processes, then you can use a Messenger to provide the interface for your service. This technique allows you to perform interprocess communication (IPC) without the need to use AIDL.
Here's a summary of how to use a Messenger:
- The service implements a
Handlerthat receives a callback for each call from a client. - The
Handleris used to create aMessengerobject (which is a reference to theHandler). - The
Messengercreates anIBinderthat the service returns to clients fromonBind(). - Clients use the
IBinderto instantiate theMessenger(that references the service'sHandler), which the client uses to sendMessageobjects to the service. - The service receives each
Messagein itsHandler—specifically, in thehandleMessage()method.In this way, there are no methods for the client to call on the service. Instead, the client delivers messages (
Messageobjects) that the service receives in itsHandler.下面的程序试验了两个功能:Activity发送消息给Service,Service产生一个Toast;Activity发送消息给Service,Service再返回一个消息给Activity
Service 代码如下:?
?
package com.example.boundservice; import android.app.Service; import android.content.Intent; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.os.Messenger; import android.os.RemoteException; import android.widget.Toast; public class MessengerService extends Service { static final int MSG_SAY_HELLO = 0; static final int MSG_FROM_SERVICE_TO_ACTIVITY = 1; // step 1 class IncomingHandler extends Handler { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub switch (msg.what) { case MSG_SAY_HELLO: Toast.makeText(getApplicationContext(), Service:Hello!, Toast.LENGTH_SHORT).show(); break; // receive activity's message and send it back; case MSG_FROM_SERVICE_TO_ACTIVITY: Messenger activityMessenger = new Messenger(new MessengerActivity().new ActivityHandler()); try { activityMessenger.send(Message.obtain(null,MessengerService.MSG_FROM_SERVICE_TO_ACTIVITY,0,0)); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; default: super.handleMessage(msg); } } } // step 2 Messenger messenger = new Messenger(new IncomingHandler()); // step 3 @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return messenger.getBinder(); } }
Activity代码如下:?
?
package com.example.boundservice; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.os.Messenger; im