AIDL——进程间通信(必须用绑定服务)
android interface defination language
使用AIDL流程
1、在服务端,创建胶水类
2、在服务端,实现onBind
3、在客户端,Activity绑定服务端的Service(通过package和action)
4、在客户端,判断是否绑定成功
5、在客户端,绑定成功时,通过胶水获取来自服务端的AIDL接口对象
注意:服务端与客户端中的AIDL接口定义完全一致
服务端
AIDLService
public class AIDLService extends Service {
// 第一步,创建胶水类
// stub:存根。已经继承了Binder的类,必须实现AIDL接口的回调方法
//stub这个特殊的胶水必须回调方法
public class MyBinder extends IMyAidlInterface.Stub {
@Override
public int plus(int plus01, int plus02) throws RemoteException {
return plus01 + plus02;
}
}
// 第二步,将胶水(含有一个回调方法的胶水)涂在Service表面
@Override
public IBinder onBind(Intent intent) {
return new MyBinder();
}
}
需要实现一个aidl接口,这个接口一般必须要在main文件夹下创建一个aidl文件夹严格区分大小写,再在此文件夹下创建一个.aidl格式的接口(File-new-下面的绿色文件里自己找).
aidl接口(当普通接口理解)
IMyAidlInterface.aidl
package org.mobiletrain.android24_aidlserver;
interface IMyAidlInterface {
int plus(int plus01, int plus02);
}
这个接口写完之后必须Build-Clean Project一下,不然找不到这个接口
清单文件里给服务做标记,不然Intent跳转不过来
<service
android:name=".AIDLService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="aidl_action"/>
</intent-filter>
</service>
客户端(Activity端)
Mianactivity
public class MainActivity extends AppCompatActivity implements ServiceConnection {
private EditText plus01EditText;
private EditText plus02EditText;
// 来自服务端中Service里的AIDL接口对象
private IMyAidlInterface aidlCallback;
private TextView resultTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
bindAIDLService();
}
// 第三步,绑定其他进程的Service
private void bindAIDLService() {
//隐式意图绑定别人家的服务
Intent intent = new Intent("aidl_action");
intent.setPackage("org.mobiletrain.android24_aidlserver");
//跳转到别人家的服务里去,高版本的时候必须注意设置包名
boolean isBindingSuccessful = bindService(intent, this, Context.BIND_AUTO_CREATE);
Toast.makeText(MainActivity.this, "是否绑定成功:" + isBindingSuccessful, Toast.LENGTH_SHORT).show();
}
private void initView() {
plus01EditText = (EditText) findViewById(R.id.plus01_et);
plus02EditText = (EditText) findViewById(R.id.plus02_et);
resultTextView = (TextView) findViewById(R.id.result_tv);
}
// 第五步,执行其他进程中的加法运算
public void plus(View view) {
String plus01String = plus01EditText.getText().toString();
String plus02String = plus02EditText.getText().toString();
if (!TextUtils.isEmpty(plus01String) && !TextUtils.isEmpty(plus02String)) {
int plus01 = Integer.parseInt(plus01String);
int plus02 = Integer.parseInt(plus02String);
try {
int result = aidlCallback.plus(plus01, plus02);
resultTextView.setText("计算结果:" + result);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
// 第四步,绑定成功时,获取Service表面的胶水(含有回调方法的胶水)
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
//固定写法获取接口对象.
aidlCallback = IMyAidlInterface.Stub.asInterface(iBinder);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
}
布局
<?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="org.mobiletrain.android24_aidlclient.MainActivity">
<EditText
android:id="@+id/plus01_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="加数1"
android:inputType="number"/>
<EditText
android:id="@+id/plus02_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="加数2"
android:inputType="number"/>
<TextView
android:id="@+id/result_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="结果"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="加法运算"
android:onClick="plus"/>
</LinearLayout>
写一个和上面那个一模一样一模一样的接口包都一样,对就是用上面的包名
IMyAidlInterface.aidl
package org.mobiletrain.android24_aidlserver;
interface IMyAidlInterface {
int plus(int plus01, int plus02);
}