前面LocalService 主要是提供同一Application中組件來使用,若是但願支持不一樣應用或進程使用Service。能夠經過Messenger。使用Messgener能夠用來支持進程間通訊而無需使用AIDL。app
下面步驟說明裏Messenger的使用方法:ide
在這種方式下,Service沒有定義能夠供Client直接調用的方法。而是經過」Message」來傳遞信息。oop
本例Messenger Service 涉及到兩個類 MessengerServiceActivities 和 MessengerService.this
首先看看Service的定義,在MessengerService定義了一個IncomingHandler ,用於處理來自Client的消息。spa
/** * Handler of incoming messages from clients. */ class IncomingHandler extends Handler { @Override public void handleMessage(Message msg) { switch (msg.what) { case MSG_REGISTER_CLIENT: mClients.add(msg.replyTo); break; case MSG_UNREGISTER_CLIENT: mClients.remove(msg.replyTo); break; case MSG_SET_VALUE: mValue = msg.arg1; for (int i=mClients.size()-1; i>=0; i--) { try { mClients.get(i).send(Message.obtain(null, MSG_SET_VALUE, mValue, 0)); } catch (RemoteException e) { // The client is dead. Remove it from the list; // we are going through the list from back to front // so this is safe to do inside the loop. mClients.remove(i); } } break; default: super.handleMessage(msg); } } }
而後使用這個IncomingHandler定義一個Messenger:rest
/** * Target we publish for clients to send messages to IncomingHandler. */ final Messenger mMessenger = new Messenger(new IncomingHandler());
應爲這種方法採用的「Bound」 Service模式,onBind 須要返回一個IBind對象, 能夠經過mMessenger.getBinder()返回與這個Messenger關聯的IBinder對象,Client能夠經過這個IBinder 對象從新構造一個Messenger對象,從而創建起與Service之間的通訊鏈路。code
/** * When binding to the service, we return an interface to our messenger * for sending messages to the service. */ @Override public IBinder onBind(Intent intent) { return mMessenger.getBinder(); }
再看看Client 的代碼MessengerServiceActivities 在 ServiceConnection的 onServiceConnected的定義,這個方法返回MessengerService 的onBind 定義的IBinder對象:對象
public void onServiceConnected(ComponentName className, IBinder service) { // This is called when the connection with the service has been // established, giving us the service object we can use to // interact with the service. We are communicating with our // service through an IDL interface, so get a client-side // representation of that from the raw service object. mService = new Messenger(service); mCallbackText.setText("Attached."); // We want to monitor the service for as long as we are // connected to it. try { Message msg = Message.obtain(null, MessengerService.MSG_REGISTER_CLIENT); msg.replyTo = mMessenger; mService.send(msg); // Give it some value as an example. msg = Message.obtain(null, MessengerService.MSG_SET_VALUE, this.hashCode(), 0); mService.send(msg); } catch (RemoteException e) { // In this case the service has crashed before we could even // do anything with it; we can count on soon being // disconnected (and then reconnected if it can be restarted) // so there is no need to do anything here. } // As part of the sample, tell the user what happened. Toast.makeText(Binding.this, R.string.remote_service_connected, Toast.LENGTH_SHORT).show(); }
本例實現了Client與Service 之間的雙向通訊,所以在Client也定義了一個Messenger對象mMessenger,用於處理來自Service的消息。blog
有了 mService對象,就能夠使用send向Service發送消息,如過須要Service 返回信息,能夠定義message.replyTo 對象。進程