进程通信有两种方式:
1使用AIDL
2使用Messenger
Messenger是一种基于消息的通信方式,主要核心是通过Handler和Message机制来通信。客户端会发送一个Message给服务端,服务端的Handler经过处理,又发送一个Message给客户端,客户端的Handler进行处理。下面是一个Messenger的例子。
服务端
package com.iiiv.aidlserver.service;
import android.app.Service;
import android.content.Intent;
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.support.annotation.Nullable;
public class MessengerService extends Service {
/**
* 标识
*/
private static final int MSG_FLAG = 0x110;
private Messenger messenger = new Messenger(new Handler() {
@Override
public void handleMessage(Message msg) {
//获取客户端传过来的对象
Message clientMsg = Message.obtain(msg);
switch (clientMsg.what) {
case MSG_FLAG:
clientMsg.what = MSG_FLAG;
try {
Thread.sleep(2000);
//做处理
String string = clientMsg.arg1 + "+" + clientMsg.arg2 + "===>>>" + (clientMsg.arg1 + clientMsg.arg2);
Bundle bundle = new Bundle();
bundle.putString("msg", string);
clientMsg.setData(bundle);
//发送给客户端
clientMsg.replyTo.send(clientMsg);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
break;
default:
break;
}
super.handleMessage(msg);
}
});
@Nullable
@Override
public IBinder onBind(Intent intent) {
return messenger.getBinder();
}
}
<service
android:name=".service.MessengerService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action
android:name="com.vvvv.message">
</action>
<category android:name="android.intent.category.DEFAULT"></category>
</intent-filter>
</service>
客户端:
package com.viii.aidlclient;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MessengerActivity extends AppCompatActivity {
private TextView textView;
private String TAG;
private static final int MSG_FLAG = 0x110;
private Messenger serviceMessenger;
private Boolean isConnSucessful;
private int count;
private Messenger clientMessanger = new Messenger(new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_FLAG:
//获取Service传来的消息并显示
String text = textView.getText().toString() + "\n" + msg.getData().getString("msg");
textView.setText(text);
break;
}
super.handleMessage(msg);
}
});
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
serviceMessenger = new Messenger(service);
Toast.makeText(MessengerActivity.this, "连接成功!", Toast.LENGTH_LONG).show();
isConnSucessful = true;
}
@Override
public void onServiceDisconnected(ComponentName name) {
Toast.makeText(MessengerActivity.this, "连接失败!", Toast.LENGTH_LONG).show();
isConnSucessful = false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_messenger);
TAG = getClass().getSimpleName();
textView = (TextView) findViewById(R.id.textView);
bindServiceInvoke();
}
/**
* 发送消息
*/
public void sendMessage(View view) {
int a = count++;
int b = (int) (Math.random() * 1000);
Message client = Message.obtain(null, MSG_FLAG, a, b);
//设置回调
client.replyTo = clientMessanger;
if (isConnSucessful) {
try {
//发送消息
serviceMessenger.send(client);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
/**
* 绑定服务
*/
private void bindServiceInvoke() {
Intent intent = new Intent();
intent.setAction("com.vvvv.message");
bindService(intent, conn, Context.BIND_AUTO_CREATE);
}
@Override
protected void onDestroy() {
//解绑服务
unbindService(conn);
super.onDestroy();
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.viii.aidlclient.MessengerActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:layout_marginTop="48dp"
tools:text="hello"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:onClick="sendMessage"
android:text="发送"
android:textAllCaps="false"
/>
</RelativeLayout>
其实Messenger的内部实现的,实际上也是依赖于aidl文件实现的,Messenger更简化了步骤,我们要根据实际情况使用Messenger和AIDL。
从AIDL的功能来看,它主要的应用场景就是IPC。虽然同一个进程中的client-service也能够通过AIDL定义接口来进行通信,但这并没有发挥AIDL的主要功能。 概括来说:
如果不需要IPC,那就直接实现通过继承Binder类来实现客户端和服务端之间的通信。
如果确实需要IPC,但是无需处理多线程,那么就应该通过Messenger来实现。Messenger保证了消息是串行处理的,其内部其实也是通过AIDL来实现。
在有IPC需求,同时服务端需要并发处理多个请求的时候,使用AIDL才是必要的
比如说播放器,放音乐在主线程容易被清理,这时候就应该放到另外一个进程。
由于Android对单个应用所使用的最大内存做了限制,为了加大一个应用可使用的内存,所以通过多进程来获取多份内存空间。
假如是这样,现在有一个项目中提供了比较成熟的计算的方法,而现在我想开发一款软件其中一个模块想用到一个计算类,而我又不想重新写了,那么就可以通过AIDL实现啦。假设,已经开发完成的那个已经提供了比较成熟的计算类的程序叫AIDLCalculateDemoServer(相当于服务器),而我要写的程序叫AIDLCalculateDemoClient(相当于客户端),类似与客户端服务器模式