最近需要用到aidl所以查找网上资料后经过不断踩坑终于测试成功,下面记一下步骤当作备忘。
-
1.首先创建一个Project,比如叫Aidl-waa,然后创建两个module:aidl和aidlservice。结构如下:
其中aidl为客户端,aidlservice为服务端,我们的目的就是aidl调用aidlservice中的方法,以此达到通讯的目的。
2.先做好服务端,
-
1.在服务端创建aidl文件IMyAidlInterface,在aidlservice右键新建即可
-
2.创建好后会生成示例方法basicTypes方法,删除即可,写一个方法getName,修改后的IMyAidlInterface如下
- 3.rebulid服务端module
- 4.创建service服务,以及内部类IBinder继承自IMyAidlInterface.Stub,重写getName方法。代码如下
public class MyService extends Service {
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
return new MyBinder();
}
public class MyBinder extends IMyAidlInterface.Stub {
@Override
public String getName() throws RemoteException {
return "success";
}
}
}
: 5.检查manifest文件中service是否注册,并添加action
<service
android:name=".MyService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="wj.com.aidlservice.MyService"/>
</intent-filter>
</service>
-
至此服务端就搞定了。
3.下面来做客户端aidl
-
1.将aidlservice下的aidl文件夹复制到aidl的main下,如图
- 2.rebuild一下aidlservice
- 3.在MainActivity中创建类MyConn实现ServiceConnection
- 4.在onServiceConnected方法中调用IMyAidlInterface.Stub.asInterface(iBinder)获取IMyAidlInterface对象
- 5.自定义initConnection方法绑定服务
-
5.在点击事件中获取MyConn对象,调用initConnection方法即可。
MainActivity代码如下
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private IMyAidlInterface iMyAidlInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btTest = (Button) findViewById(R.id.bt_test);
btTest.setOnClickListener(this);
}
public void onClick(View view) {
MyConn conn = new MyConn();
conn.initConnection();
}
private class MyConn implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
iMyAidlInterface = IMyAidlInterface.Stub.asInterface(iBinder);
try {
Toast.makeText(MainActivity.this, iMyAidlInterface.getName(), Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
iMyAidlInterface = null;
}
void initConnection() {
Intent intent = new Intent();
intent.setAction("wj.com.aidlservice.MyService");
intent.setPackage("wj.com.aidlservice");
bindService(intent, this, BIND_AUTO_CREATE);
}
}
}
布局如下
<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="wj.com.aidl.MainActivity">
<Button
android:id="@+id/bt_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="aidl" />
</LinearLayout>
注意事项:
1.服务端service必须在manifest中注册,并设置action
2.服务端必须启动,客户端才能调取服务中的方法
3.由于5.0开始服务必须显示调用,setAction()要与service在manifest中注册的action相同,setPackage为服务端的包名。
否则报错java.lang.IllegalArgumentException: Service Intent must be explicit: Intent。
4.aidl文件保持一样,记得rebuild。
以上就是aidl的简单使用了,使用到的代码已经全部贴上,
我的Androidstudio版本2.2,手机系统7.0,亲测有效。
qq群492462202欢迎交流。
参考资料:
本篇整体思路流程参考自该博客,非常感谢!
http://www.jianshu.com/p/d1fac6ccee98
感觉有帮助就赞助一下吧