关于进程间通信,很多人会说AIDL,但那个太复杂了,也很少用,我们不妨用Messenger来实现简单的进程间通信吧。至于AIDL更适用于多线程复杂数据的通信!
使用Messenger的步骤:
1.首先使服务实现一个Handler,由其接受来之客户端的每个调用的回调
2. Handler用于创建Messenger对象(对Handler的引用)
3.Messenger创建一个IBinder对象,服务通过onBind()方法返回给客户端
4.客户端使用IBinder把Messenger实例化,然后通过使用后者把Message发送给服务
5.服务在自己的Handler对象的handleMessage()方法中,来接收这个Message对象
这样,客户端并没有调用服务的方法,服务也接收到了客户端发送的Message对象,以上的关于Messenger使用步骤
来至google官方文档:https://developer.android.com/guide/components/bound-services.html#Binding
我看了这个使用步骤,还是不明白具体怎样实现进程间通信,我们还是以实际的例子来感受一下吧!
public class MessengerService extends Service {
/** Command to the service to display a message */
static final int MSG_SAY_HELLO = 1;
/**
* Handler of incoming messages from clients.
*/
class IncomingHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_SAY_HELLO:
Toast.makeText(getApplicationContext(), "hello!", Toast.LENGTH_SHORT).show();
break;
default:
super.handleMessage(msg);
}
}
}
/**
* Target we publish for clients to send messages to IncomingHandler.
*/
final Messenger mMessenger = new Messenger(new IncomingHandler());
/**
* When binding to the service, we return an interface to our messenger
* for sending messages to the service.
*/
@Override
public IBinder onBind(Intent intent) {
Toast.makeText(getApplicationContext(), "binding", Toast.LENGTH_SHORT).show();
return mMessenger.getBinder();
}
}
在这里我们看到了如官方文档中的步骤1中所说实现了一个Handler对象:IncomingHandler,然后实现步骤2的创建Messenger对象,并把IncomingHandler以构造参数的方式传递进去,这就是所谓的对Handler的引用。然后实现步骤3在服务onBind()方法中:通过此Messenger的实例化对象mMessenger的getBinder()方法得到一个IBind对象,并返回给此onBind。这样客户端就可以通过与服务的绑定后的连接方法中得到此IBinder对象,并通过IBInder对象创建Messenger,最后用send()方法发送消息给服务就可以了!
前面三个步骤,我们通过官方代码已经基本了解了它说的意思!那后面的呢,我们还是通过代码来一探究竟!
public class ActivityMessenger extends Activity {
/** Messenger for communicating with the service. */
Messenger mService = null;
/** Flag indicating whether we have called bind on the service. */
boolean mBound;
/**
* Class for interacting with the main interface of the service.
*/
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
// This is called when the connection with the service has been
// established, giving us the object we can use to
// interact with the service. We are communicating with the
// service using a Messenger, so here we get a client-side
// representation of that from the raw IBinder object.
mService = new Messenger(service);
mBound = true;
}
public void onServiceDisconnected(ComponentName className) {
// This is called when the connection with the service has been
// unexpectedly disconnected -- that is, its process crashed.
mService = null;
mBound = false;
}
};
public void sayHello(View v) {
if (!mBound) return;
// Create and send a message to the service, using a supported 'what' value
Message msg = Message.obtain(null, MessengerService.MSG_SAY_HELLO, 0, 0);
try {
mService.send(msg);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
protected void onStart() {
super.onStart();
// Bind to the service
bindService(new Intent(this, MessengerService.class), mConnection,
Context.BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.onStop();
// Unbind from the service
if (mBound) {
unbindService(mConnection);
mBound = false;
}
}
}
在这个活动中,我们看到在onStart()方法中我们通过自动创建服务的方法绑定了服务,这里自然要用到服务连接mConnection对象,并在onStop()方法中解绑服务。官方代码也告诉了我们服务的绑定与解绑是成对出现的,并在什么地方使用绑定和解绑的方法,以前我一直都是在onCreate方法中绑定服务,在onDestroy中解绑服务!其实我们应该在onStop中解绑的,因为在onStop状态时,说明活动已经完全被覆盖,这个时候我们还是连接到服务更新数据已经失去了意义,因为就算更新了数据到活动,用户也看不到,那不是浪费系统资源吗?圆规正传,我们说到步骤4在客服端创建Messenger对象,这里是通过连接服务成功的方法onServiceConnection()方法得到服务返回的IBinder对象,并以构造参数的形式来创建Messenger对象的。
注意:a.在服务中我们是通过new Messenger(new IncomingHandler()),构造方法里面的参数为Handler对象。b.而在客户端我们是通过
new Messenger(service)来创建Messenger对象,构造方法中是IBinder对象。在连接成功方法中,我们创建了Messenger对象,并把绑定状态设置为true。断开连接中,我们把绑定状态设置为false,并制空Messenger对象。这里我们看到sayHello方法是来发送消息的,首先通过Message.obtain()方法创建了Message对象,里面的第二个参数就是服务要接受的Message.what,这里我们用服务设置好的常量MSG_SAY_HELLO,最后通过Messenger对象mService的send(msg)方法来发送这个Message。
步骤5其实最后要回到服务的handleMessage方法中,通过接受到来之客户端的信息,通过what参数来区分后,弹出一个提示!
至此,我们关于通过Messenger来实现进程间通信就说晚了,比较啰嗦。但是我相信你看完了还是觉得一头雾水,其实很多东西要理解,关键还是自己去实践一把最实在!我写这些也是让自己更深入的理解一下官方文档的意思,并不是让多少读者来认同我的观点。有不同观点的同学,可以留言,相互学习成长!