简易 AIDL:Messenger 信使使用

AIDL 完全实现的话 实在太费劲了,好在 Android 提供了 Messenger 来简化跨进程的使用。注意不是 Message。


public class MessengerService extends Service {

//最好换成HandlerThread的形式

private Messenger mMessenger = new Messenger(new Handler(Looper.getMainLooper()) {

@Override

public void handleMessage(Message msgfromClient) {

Log.i("hhh", "MessengerService : handleMessage: " );

switch (msgfromClient.what) {

//msg 客户端传来的消息

case 911:

Log.i("hhh", "MessengerService : handleMessage:收到消息了" );

App.goAhead();

}

super.handleMessage(msgfromClient);

}

});

@Override

public IBinder onBind(Intent intent) {

Log.i("hhh", "MessengerService : onBind:>>> " );

return mMessenger.getBinder();

}

@Override

public void onCreate() {

super.onCreate();

Log.i("hhh", "MessengerService : onCreate: >>>" );

}

}