Messenger:信使
官方文档解释:它引用了一个Handler对象,以便others能够向它发送消息(使用mMessenger.send(Message msg)方法)。该类允许跨进程间基于Message的通信(即两个进程间可以通过Message进行通信),在服务端使用Handler创建一个Messenger,客户端持有这个Messenger就可以与服务端通信了。
以前我们使用Handler+Message的方式进行通信,都是在同一个进程中,从线程持有一个主线程的Handler对象,并向主线程发送消息。
而Android既然可以使用bindler机制进行跨进行通信,所以我们当然可以将Handler与bindler结合起来进行跨进程发送消息。
View Code
package com.xwangly.apidemo.app; import java.util.Random; 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.util.Log; public class MessengerService extends Service { private String TAG = "MessengerService"; @Override public void onDestroy() { // TODO Auto-generated method stub Log.i(TAG, "onDestroy"); cMessenger = null; super.onDestroy(); } @Override public boolean onUnbind(Intent intent) { // TODO Auto-generated method stub Log.i(TAG, "onUnbind"); return super.onUnbind(intent); } static final int MSG_REGISTER_CLIENT = 1; static final int MSG_UNREGISTER_CLIENT = 2; static final int MSG_SET_VALUE = 3; private Random random = new Random(); private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub Log.i(TAG, "handleMessage"); switch (msg.what) { case MSG_SET_VALUE: try { Message message = Message.obtain(null, MessengerService.MSG_SET_VALUE); message.arg1 = random.nextInt(100); //得到客户端的信使对象,并向它发送消息 cMessenger = msg.replyTo; cMessenger.send(message); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; default: super.handleMessage(msg); } } }; /** * 自己的信使对象 */ private Messenger mMessenger = new Messenger(mHandler); /** * 客户的信使 */ private Messenger cMessenger; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub Log.i(TAG, "onBind"); //返回自己信使的bindler,以供客户端通过这个bindler得到服务端的信使对象(通过new Messenger(bindler)) return mMessenger.getBinder(); } @Override public void onRebind(Intent intent) { // TODO Auto-generated method stub Log.i(TAG, "onRebind"); } }
View Code
package com.xwangly.apidemo.app; import com.xwangly.apidemo.R; import android.app.Activity; import android.content.ComponentName; 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; import android.os.RemoteException; import android.util.Log; import android.view.View; import android.widget.TextView; public class MessengerServiceActivities { public static class Binding extends Activity implements View.OnClickListener { private String TAG = "Binding"; TextView mCallbackText; private boolean isBound; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.messenger_service_binding); findViewById(R.id.bind).setOnClickListener(this); findViewById(R.id.unbind).setOnClickListener(this); mCallbackText = (TextView) findViewById(R.id.callback); mCallbackText.setText("Not attached."); } private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { Log.i(TAG, "handleMessage"); switch (msg.what) { case MessengerService.MSG_SET_VALUE: mCallbackText.setText("Received from service: " + msg.arg1); break; default: super.handleMessage(msg); } } }; /** * 自己的信使 */ private Messenger mMessenger; /** * 远程服务的信使 */ private Messenger rMessenger; private ServiceConnection connection = new ServiceConnection() { public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub Log.i(TAG, "onServiceConnected"); rMessenger = new Messenger(service); mMessenger = new Messenger(mHandler); sendMessage(); } public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub rMessenger = null; } }; public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent( "com.xwangly.apidemo.app.messenger_service"); switch (v.getId()) { case R.id.bind: if (!isBound) { isBound = bindService(intent, connection, BIND_AUTO_CREATE); //isBound = true; }else { sendMessage(); } break; case R.id.unbind: if (isBound) { unbindService(connection); isBound = false; } break; default: break; } } /** * 使用服务端的信使向它发送一个消息。 */ private void sendMessage() { // TODO Auto-generated method stub Message message = Message.obtain(null, MessengerService.MSG_SET_VALUE); message.replyTo = mMessenger; try { rMessenger.send(message); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
View Code
<service android:name=".app.MessengerService" > <intent-filter> <action android:name="com.xwangly.apidemo.app.messenger_service" /> </intent-filter> </service> <activity android:name=".app.MessengerServiceActivities$Binding" android:label="@string/activity_messenger_service_binding" android:launchMode="singleTop"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>