服务端:
启动AS,创建一个服务端工程,在工程中点击右键New->AIDL->AIDL File:
我们打开这个aidl文件,在其中添加一个测试方法:

此时编译工程,在如下路径自动生成一个aidl对应的java文件(该java类的代码就是整个Binder机制的原理所在):
创建一个服务类,继承Service:
package sources.cn.binderserviceprj;
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;
/**
* Created by Administrator on 2018/1/24.
*/
public class MyService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return myService;
}
private IBinder myService = new IMyAidlInterface.Stub() {
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
}
@Override
public int add(int num1, int num2) throws RemoteException {
Log.d("test","==num1==>" + num1 + ", ====num2===>" + num2);
return num1 + num2;
}
};
}
<!--exported:允许外界访问该服务,AIDL必备条件-->
<service android:name=".MyService"
android:exported="true"/>
最后主程序中启动该服务:public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService(new Intent(this,MyService.class));
}
}
客户端:
在工程中点击右键New->Module:
进入工程所在的目录,将服务端的aidl目录完全拷贝到客户端对应的目录下:
同样,此时需要编译一次客户端代码,好了此时编写客户端代码:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/binderBtn"
android:layout_width="match_parent"
android:layout_height="100dp" />
</LinearLayout>
MainActivity.javapackage sources.cn.binderclient;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import sources.cn.binderserviceprj.IMyAidlInterface;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
IMyAidlInterface iMyAidlInterface;
private Button binderBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent();
intent.setComponent(new ComponentName("sources.cn.binderserviceprj","sources.cn.binderserviceprj.MyService"));
bindService(intent,conn,BIND_AUTO_CREATE);
binderBtn = (Button) findViewById(R.id.binderBtn);
binderBtn.setOnClickListener(this);
}
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
iMyAidlInterface = IMyAidlInterface.Stub.asInterface(iBinder);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
iMyAidlInterface = null;
}
};
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(conn);
}
@Override
public void onClick(View view) {
try{
int result = iMyAidlInterface.add(1,2);
Log.d("test","=======result=====" + result);
}catch (RemoteException e){
e.printStackTrace();
}
}
}
测试结果:
至此,简单实现进程间通信