port android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
public class MessengerActivity extends Activity implements ServiceConnection{
private Messenger messenger;
private Boolean mBound = false;
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
bindService(new Intent(MessengerActivity.this,MessengerService.class),this, Context.BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
if(!mBound) {
unbindService(this);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_messenger_activity);
//send message to service
findViewById(R.id.button_send_message_to_service).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
try {
// a message is a reference to the Handler
messenger.send(Message.obtain(null,MessengerService.MSG_SAY_HELLO,0,0));
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
// send message to the service to trigger a message to be sent back;
findViewById(R.id.button_reveive_message_to_service).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
try {
// a message is a reference to the Handler
messenger.send(Message.obtain(null,MessengerService.MSG_FROM_SERVICE_TO_ACTIVITY,0,0));
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
// A handler used to receive message from service
public class ActivityHandler extends Handler {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
if(msg.what == MessengerService.MSG_FROM_SERVICE_TO_ACTIVITY) {
Log.i(FFFF,Received Service's Message!);
}
}
}
@Override
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
// a message is a reference to the Handler
// use a messenger to wrap the binder,so can we send the message to service
messenger = new Messenger(arg1);
mBound = true;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
}
?
Messenger和AIDL的比较:
?
Compared to AIDL
?
When you need to perform IPC, using a Messenger for your interface is simpler than implementing it with AIDL, because Messenger queues all calls to the service, whereas, a pure AIDL interface sends simultaneous requests to the service, which must then handle multi-threading.
For most applications, the service doesn't need to perform multi-threading, so using a Messengerallows the service to handle one call at a time. If it's important that your service be multi-threaded, then you should use AIDL to define your interface.
Binding to a Service
Application components (clients) can bind to a service by calling bindService(). The Android system then calls the service's onBind() method, which returns an IBinder for interacting with the service.
The binding is asynchronous. bindService() returns immediately and does not return the IBinder to the cl