对于AIDL的一些使用:最基础使用、稍高级使用、......java
<!--more-->android
此章节可能和本文没有太大关系,了解一下便可git
基本开发流程:先开发Service端,后开发Client端github
使用AndroidStudio建立AidlDemo工程后,再在里面建立Service Module和Client Module,不用管默认的app Modulebash
分支:feature/aidl_baseapp
包名:com.fqxyi.aidlserviceide
一、建立aidl文件,如:IAidlBinder.aidl
,新增接口,如:String getInfo();
函数
二、检查build/generated/source/aidl/debug
下是否存在对应的java文件,若无则Rebuild Project
,若出现错误,请查看#FAQ章节ui
三、建立继承于android.app.Service的Service类,如:AidlService.java
,并实现必需要实现的onBind方法this
3.一、在AndroidManifest.xml文件中静态注册Service,详细注册代码以下:
<!-- exported表示是否支持其它应用调用当前组件 --> <!-- process表示将组建运行到单独的进程中 --> <!-- action用于用于过滤识别其余的Intent --> <service android:name=".AidlService" android:exported="true" android:process=":Remote"> <intent-filter> <action android:name="com.fqxyi.aidlservice.remote"/> </intent-filter> </service>
四、接着咱们回到AidlService.java文件,onBind方法须要咱们返回一个IBinder对象。显然,到目前为止可以获得IBinder对象的类只有经过IAidlBinder.aidl自动生成的IAidlBinder.java类。
因为默认代码格式很乱,因此为了方便查看,咱们可使用快捷键格式化一下代码:Ctrl(Command)+Alt(Option)+L
仔细阅读代码发现,咱们想要获得的IBinder对象是经过asBinder()方法返回的,因此接下来咱们只须要返回一个IAidlBinder.Stub的对象就能够了。
五、实例化IAidlBinder.Stub的对象以后,咱们能够处理咱们自定义的方法getInfo(),好比最简单的就是返回一串字符串:return "I'm a Server";
包名:com.fqxyi.aidlclient
一、将Service端的aidl文件,拷贝到main文件夹下,须要注意的是aidl文件的包名仍是Service端的包名,具体目录结构以下:
aidlclient |--src |--main |--aidl |--com.fqxyi.aidlservice // Service端的包名 |--IAidlBinder.aidl
二、检查build/generated/source/aidl/debug
下是否存在对应的java文件,若无则Rebuild Project
,若出现错误,请查看#FAQ章节
三、建立Intent对象并实例化,接着配置在Service端配置的action
,实现Service的绑定,具体代码以下因此:
Intent intent = new Intent(); intent.setAction("com.fqxyi.aidlservice.remote"); intent.setPackage("com.fqxyi.aidlservice"); bindService(intent, conn, Context.BIND_AUTO_CREATE);
四、上述代码因不存在ServiceConnection而报错,因此很简单,咱们须要建立一个ServiceConnection对象并实例化,接着在必需要实现的onServiceConnected(ComponentName name, IBinder service)方法中初始化IAidlBinder
,在onServiceDisconnected(ComponentName name)方法中将IAidlBinder置为null
。
仔细阅读IAidlBinder.java代码发现,咱们想要获得的IAidlBinder对象是经过asInterface(android.os.IBinder obj)方法返回的,须要传入一个IBinder对象,因此接下来就很简单了,只须要以下代码便可:
IAidlBinder.Stub.asInterface(service);
五、最后咱们只须要经过第4步获得的IAidlBinder对象,调用getInfo()方法,就能够获得内容。
分支:feature/aidl_advanced
此处文档说明基于#开发流程-最基础使用章节,经过列出不一样点进行说明。
包名:com.fqxyi.aidlservice
一、建立aidl文件,如:IAidlBinder.aidl
,新增接口,如:String getInfo();
和Student getStudentInfo();
须要注意的是Student是咱们本身定义的一个实现了Parcelable接口的Model类,在aidl文件中定义接口,须要咱们手动import语句,添加引用。
二、建立Student类,具体代码以下所示:
package com.fqxyi.aidlservice.model; import android.os.Parcel; import android.os.Parcelable; /** * Created by qingfeng on 2017/7/27. */ public class Student implements Parcelable { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } protected Student(Parcel in) { this.name = in.readString(); this.age = in.readInt(); } public static final Creator<Student> CREATOR = new Creator<Student>() { @Override public Student createFromParcel(Parcel in) { return new Student(in); } @Override public Student[] newArray(int size) { return new Student[size]; } }; @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(name); dest.writeInt(age); } }
你们若是不深究、只考虑使用的话,不用担忧本身会不会写这些代码,由于主要代码是自动生成
的。
咱们只须要定义一个name和age属性,而后执行如下两部操做就好了:
三、建立Student.aidl文件,声明Student实现了Parcelable接口,具体代码以下所示:
// Student.aidl package com.fqxyi.aidlservice.model; // Declare any non-default types here with import statements import com.fqxyi.aidlservice.model.Student; parcelable Student;
四、检查build/generated/source/aidl/debug
下是否存在对应的java文件,若无则Rebuild Project
,若出现错误,请查看#FAQ章节
三、建立继承于android.app.Service的Service类,如:AidlService.java
,并实现必需要实现的onBind方法
3.一、在AndroidManifest.xml文件中静态注册Service,详细注册代码以下:
<!-- exported表示是否支持其它应用调用当前组件 --> <!-- process表示将组建运行到单独的进程中 --> <!-- action用于用于过滤识别其余的Intent --> <service android:name=".AidlService" android:exported="true" android:process=":Remote"> <intent-filter> <action android:name="com.fqxyi.aidlservice.remote"/> </intent-filter> </service>
四、接着咱们回到AidlService.java文件,onBind方法须要咱们返回一个IBinder对象。显然,到目前为止可以获得IBinder对象的类只有经过IAidlBinder.aidl自动生成的IAidlBinder.java类。
因为默认代码格式很乱,因此为了方便查看,咱们可使用快捷键格式化一下代码:Ctrl(Command)+Alt(Option)+L
仔细阅读代码发现,咱们想要获得的IBinder对象是经过asBinder()方法返回的,因此接下来咱们只须要返回一个IAidlBinder.Stub的对象就能够了。
五、实例化IAidlBinder.Stub的对象以后,咱们能够处理咱们自定义的方法getInfo(),好比最简单的就是返回一串字符串:return "I'm a Server";
若是咱们想要处理getStudentInfo()方法的话,能够在onCreate()方法中进行初始化赋值操做,而后在getStudentInfo()方法中直接return便可。
包名:com.fqxyi.aidlclient
一、将Service端的aidl文件、和须要调用的文件,如:Student.java
,拷贝到main文件夹下,须要注意的是这些文件的包名仍是Service端的包名,具体目录结构以下:
aidlclient |--src |--main |--aidl |--com.fqxyi.aidlservice // Service端的包名 |--model |--Student.aidl |--IAidlBinder.aidl |--java |--com.fqxyi.aidlservice // Service端的包名 |--model |--Student.java
二、检查build/generated/source/aidl/debug
下是否存在对应的java文件,若无则Rebuild Project
,若出现错误,请查看#FAQ章节
三、建立Intent对象并实例化,接着配置在Service端配置的action
,实现Service的绑定,具体代码以下因此:
Intent intent = new Intent(); intent.setAction("com.fqxyi.aidlservice.remote"); intent.setPackage("com.fqxyi.aidlservice"); bindService(intent, conn, Context.BIND_AUTO_CREATE);
四、上述代码因不存在ServiceConnection而报错,因此很简单,咱们须要建立一个ServiceConnection对象并实例化,接着在必需要实现的onServiceConnected(ComponentName name, IBinder service)方法中初始化IAidlBinder
,在onServiceDisconnected(ComponentName name)方法中将IAidlBinder置为null
。
仔细阅读IAidlBinder.java代码发现,咱们想要获得的IAidlBinder对象是经过asInterface(android.os.IBinder obj)方法返回的,须要传入一个IBinder对象,因此接下来就很简单了,只须要以下代码便可:
IAidlBinder.Stub.asInterface(service);
五、最后咱们只须要经过第4步获得的IAidlBinder对象,调用getInfo()方法,就能够获得内容,也能够调用getStudentInfo()方法,得到Student对象。
Error:Execution failed for task ':aidlservice:compileDebugAidl'. > java.lang.RuntimeException: com.android.ide.common.process.ProcessException: Error while executing process /qingfeng/work/sdk/build-tools/25.0.2/aidl with arguments {-p/qingfeng/work/sdk/platforms/android-25/framework.aidl -o/qingfeng/data/openSource/AidlDemo/aidlservice/build/generated/source/aidl/debug -I/qingfeng/data/openSource/AidlDemo/aidlservice/src/main/aidl -I/qingfeng/data/openSource/AidlDemo/aidlservice/src/debug/aidl -I/Users/qingfeng/.android/build-cache/92fb7eb4401d63eb124015b36c2a8a534302f1c9/output/aidl -d/var/folders/tq/f6kngw516g15xs24gdh80qvh0000gn/T/aidl1446680322459273337.d /qingfeng/data/openSource/AidlDemo/aidlservice/src/main/aidl/com/fqxyi/aidlservice/IAidlBinder.aidl}
出现此类问题,实际上是由于你的aidl文件编写错误,请仔细检查:
FATAL EXCEPTION: main Process: com.fqxyi.aidlclient, PID: 19282 java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.fqxyi.aidlservice.remote } at android.app.ContextImpl.validateServiceIntent(ContextImpl.java:1219) at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1318) at android.app.ContextImpl.bindService(ContextImpl.java:1296) ...
若是你使用的Android设备的版本大于5.0,则须要在bindService的时候,为你的intent添加如下语句:
intent.setPackage("com.fqxyi.aidlservice");
什么?
代码太多?
做者写的太烂?看的头昏脑涨...
不要紧,直接去看源码就好了,记住:
最基础使用分支:feature/aidl_base,传送门
稍高级使用分支:feature/aidl_advanced,传送门