一、原因:
Android每个应用因为有独立的虚拟机,独立的内存空间,所以不能通过共享内存来进行数据交互。
其中有两种交互方式,一种是RPC另一种是IPC,RPC是指直接调用其他应用的方法,IPC只是进行进程间的通讯。
二:常见的进程间通讯的方式:
Bundle、Messager、AIDL、ContentProvider、Socket、Broadcast
三:AIDL
1.什么是AIDL:Android Interface Definition Language
2.AIDL使用步骤:
服务端3步:
a.新建aidl文件
b.新建service绑定aidl
c.manifest中注册sevice
客户端3步:
a.首先把服务端的aidl文件连同包名copy过来
b.绑定服务端的service,5.0之后在绑定时要设置包名
c.调用aidl中的方法实现客户端功能
------------------------------------------------------------------------
服务端①>>> a.新建aidl文件
// IMyAidlInterfaceTest.aidl
package test.aidl;
// Declare any non-default types here with import statements
interface IMyAidlInterfaceTest {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
double doCalculate(double a, double b);
}
------------------------------------------------------------------------
服务端②>>> b.新建service绑定aidl
package aidl.test.com.testaidlservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.util.Log;
import test.aidl.IMyAidlInterfaceTest;
public class MyAidlService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public void onCreate() {
super.onCreate();
Log.e("Test", "绑定成功!");
}
private final IMyAidlInterfaceTest.Stub mBinder = new IMyAidlInterfaceTest.Stub() {
@Override
public double doCalculate(double a, double b) throws RemoteException {
CalculateNumber calculateNumber = new CalculateNumber();
return calculateNumber.calculateNum(a, b);
}
};
}
------------------------------------------------------------------------
服务端③>>> c.manifest中注册sevice
------------------------------------------------------------------------
------------------------------------------------------------------------
客户端①>>> a.首先把服务端的aidl文件连同包名copy过来
// IMyAidlInterfaceTest.aidl
package test.aidl;
// Declare any non-default types here with import statements
interface IMyAidlInterfaceTest {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
double doCalculate(double a, double b);
}
------------------------------------------------------------------------
客户端②>>> b.绑定服务端的service,5.0之后在绑定时要设置包名
private IMyAidlInterfaceTest myAidl;
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
Log.e("Test", "客户端onServiceConnected");
myAidl = IMyAidlInterfaceTest.Stub.asInterface(iBinder);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
Log.e("Test", "客户端onServiceDisconnected");
myAidl = null;
}
};
//绑定服务
Intent intent = new Intent("aidl.test.com.testaidlservice.MyAidlService");
intent.setPackage("aidl.test.com.testaidlservice");
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
------------------------------------------------------------------------
客户端③>>> c.调用aidl中的方法实现客户端功能
double a = 5;
double b = 8;
try {
Log.e("Test", "测试结果:" + myAidl.doCalculate(a, b));
} catch (RemoteException e) {
e.printStackTrace();
}
------------------------------------------------------------------------