aidl: Android Interface definition language 安卓接口定义语言;
- aidl是 IPC:(Inter Process Communication(进程间的通讯))的方法之一。
首先说清两个概念:
本地服务:写在自己的应用程序的工程里的服务 ,使用自己应用程序的进程运行这个服务;
远程服务:写在别的应用程序的工程里的服务,使用别的应用程序的进程运行这个服务(安装在同一个手机上的应用程序);
步骤:
1、创建一个服务的接口类,里面包含需要对外暴露的业务逻辑方法:
2、让服务中的中间人实现了服务的接口类:
3、修改并拷贝接口文件:
4、在本地服务的工程中的activity里,绑定服务:
5、通过接口调用远程服务的方法:
一.在Android studio中,首先创建aidl文件
步骤:File –> New –> AIDL –> AIDL File
然后可以看到在src/main目录下生成了一个目录aidl,点开aidl,可以看到刚才创建的IMyAidlInterface.aidl文件
二.②编译(因为Android Studio不能像Eclipse那样自动编译aidl文件,所以需要手动编译)
点击Build –> Make Module
然后可以看到在build/generated/source/debug/包名/目录下生成了一个文件Iservice,表示编译成功;
三.编写Service
package com.zhonghao.aidle;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
/**
* 项目名称:PassingTime
* 类描述:
* 创建人:小豪
* 创建时间:2017/4/20 16:03
* 修改人:小豪
* 修改时间:2017/4/20 16:03
* 修改备注:
*/
public class RemoteService extends Service {
//2.把我们定义的中间人返回
@Nullable
@Override
public IBinder onBind(Intent intent) {
return new MyBinder();
}
//在服务里定义一个方法
public void testMethod(){
System.out.println("我是远程服务力的一个方法");
}
//1.定义一个中间人对象
private class MyBinder extends Iservice.Stub{
@Override
public void callTestMethod() {
testMethod();
}
}
}
四.在AndroidManifest.xml中进行配置:
<service
android:name=".RemoteService">
<intent-filter>
<action android:name="com.zhonghao.localservice"/>
</intent-filter>
</service>
到此为止,被调用的远程服务已经配置完毕
调用远程服务的项目配置如下:
一.需要将服务端配置的整个aidl目录一并拷贝到客户端所在工程src/main目录下(包名与文件名必须与服务端一模一样)
二.编译
(结果和被调用端一样自动生成文件)
三.绑定服务
package com.zhonghao.localservice;
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.view.View;
import com.zhonghao.aidle.Iservice;
public class MainActivity extends AppCompatActivity {
private MyConn mMyConn;
private Iservice mIservice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1调用bindService,获取我们定义的中间人对象
Intent intent = new Intent();
//隐式调用,设置一个action
intent.setAction("com.zhonghao.localservice");
//从 Android 5.0开始 隐式Intent绑定服务的方式已不能使用,所以这里需要设置Service所在服务端的包名
intent.setPackage("com.zhonghao.aidle");
mMyConn = new MyConn();
//2目的是为了获取我们定义的中间人对象
bindService(intent, mMyConn,BIND_AUTO_CREATE);
}
public void click(View v){
try {
mIservice.callTestMethod();
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(mMyConn);
}
private class MyConn implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//获取中间人对象
mIservice = Iservice.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
}
其中通过 mIservice = Iservice.Stub.asInterface(service);以获取到服务端的代理对象,从而可以通过代理对象调用方法从被调用的远程服务端获取到我们所需的数据。
AIDL的应用场景
- 支付宝 (暴露一个支付的方法)