1:首先鼠标右键点击项目新建AIDL Folder
2:在 aidl文件右键新建AIDL File
3:删除 或者无视里面的内容,并写下如下代码,并Rebuild project。
package com.dt.aidltest;
interface IMyAidlInterface {
int add(int num1,int num2);
}
4:新建一个Service ,主要是用来实现AIdl,并把它暴露给客户端,以方便客户端调用
public class AIDLService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return new IMyAidlInterface.Stub() {
@Override
public int add(int num1, int num2) throws RemoteException {
return num1 + num2;
}
};
}
}
5:在 AndroidManifest 中 声明该Service 并添加 action ,这里好向必须添加 action,否则在客户端会报空指针异常。
<service android:name=".AIDLService">
<intent-filter>
<action android:name="android.intent.action.AIDLServoce" />
</intent-filter>
</service>
6:点击File,新建一个module (客户端)
7:在客户端新建AIDL 文件夹,并把服务端的aidl文件copy到客户端,包括包名,要保持一致。最后在 rebuild project 。
8:接下来就简单了,首先绑定service,通过service,拿到AIDL接口,IMyAidlInterface。
Intent intent = new Intent();
intent.setAction("android.intent.action.AIDLServoce");
bindService(intent, conn, BIND_AUTO_CREATE);
protected IMyAidlInterface iMyAidlInterface;
protected ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
iMyAidlInterface = IMyAidlInterface.Stub.asInterface(iBinder);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
};
9:拿到 iMyAidlInterface 后,就可以调用它的方法了
try {
int result = iMyAidlInterface.add(num1,num2);
edt3.setText(result+"");
} catch (RemoteException e) {
e.printStackTrace();
}
最后,客户端代码如下:
MainActivity 的代码 如下:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
protected EditText edt1;
protected EditText edt2;
protected Button btnAdd;
protected EditText edt3;
protected IMyAidlInterface iMyAidlInterface;
protected ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
iMyAidlInterface = IMyAidlInterface.Stub.asInterface(iBinder);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
};
private int num1;
private int num2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
initView();
initService();
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.btn_add) {
getNums();
try {
int result = iMyAidlInterface.add(num1,num2);
edt3.setText(result+"");
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
private void getNums() {
num1 = Integer.parseInt(edt1.getText().toString().trim());
num2 = Integer.parseInt(edt2.getText().toString().trim());
}
@Override
protected void onDestroy() {
super.onDestroy();
unbindService();
}
private void initService() {
Intent intent = new Intent();
intent.setAction("android.intent.action.AIDLServoce");
bindService(intent, conn, BIND_AUTO_CREATE);
}
private void unbindService() {
unbindService(conn);
conn = null;
}
private void initView() {
edt1 = (EditText) findViewById(R.id.edt1);
edt2 = (EditText) findViewById(R.id.edt2);
btnAdd = (Button) findViewById(R.id.btn_add);
btnAdd.setOnClickListener(MainActivity.this);
edt3 = (EditText) findViewById(R.id.edt3);
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.dt.aidl_client.MainActivity">
<EditText
android:id="@+id/edt1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:textSize="30dp" />
<EditText
android:id="@+id/edt2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="调用AIDL来计算" />
<EditText
android:id="@+id/edt3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false" />
</LinearLayout>