AIDL实例

服务端:

1. 创建要传递的实体类 Person,并实现 Parcelable 接口。

package cn.zzw.aidl;

import android.os.Parcel;
import android.os.Parcelable;

public class PersonInfo implements Parcelable{
private String name;
private int age;
private String sex;
private float height;
public PersonInfo(String name, int age, String sex, float height) {
this.name = name;
this.age = age;
this.sex = sex;
this.height = height;
}

public PersonInfo() {

}

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;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

public float getHeight() {
return height;
}

public void setHeight(float height) {
this.height = height;
}

public static Creator<PersonInfo> getCREATOR() {
return CREATOR;
}

protected PersonInfo(Parcel in) {
name = in.readString();
age = in.readInt();
sex = in.readString();
height = in.readFloat();
}

public static final Creator<PersonInfo> CREATOR = new Creator<PersonInfo>() {
@Override
public PersonInfo createFromParcel(Parcel in) {
return new PersonInfo(in);
}

@Override
public PersonInfo[] newArray(int size) {
return new PersonInfo[size];
}
};

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(age);
dest.writeString(sex);
dest.writeFloat(height);
}
}
注意:此文件所在的包名要和 .aidl 文件所在的包名一样。

2. 创建 IPerson.aidl 文件

// IPerson.aidl
package cn.zzw.aidl;
import cn.zzw.aidl.PersonInfo;
// Declare any non-default types here with import statements
interface IPerson {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
String sayHello(in PersonInfo personInfo);
int sumnNum(int num1,int num2);
}
注意: 需要import 相关的实体类,否则会报如下错误:

Process 'command 'D:\Android\sdk\build-tools\28.0.3\aidl.exe'' finished with non-zero exit value 1
3. 对于实体类 Person 也必须创建对应的 .aidl 文件

// PersonInfo.aidl
package cn.zzw.aidl;

parcelable PersonInfo;
以上三步创建后,目录如下:

 

4. 创建隐式意图的Service(对于此文件所在的包没有要求),并在onBind方法返回已经实现AIdl 接口的对象。

package cn.zzw.aidlserverdemo.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;

import cn.zzw.aidl.IPerson;
import cn.zzw.aidl.PersonInfo;

public class AidlServerService extends Service {

private class MyBinder extends IPerson.Stub{
@Override
public String sayHello(PersonInfo personInfo) throws RemoteException {
if(null!=personInfo)
{
String name = personInfo.getName();
int age = personInfo.getAge();
float height = personInfo.getHeight();
String sex = personInfo.getSex();
return "Hello,My Name is "+name;
}
return null;
}

@Override
public int sumnNum(int num1, int num2) throws RemoteException {
return num1+num2;
}
};
@Override
public IBinder onBind(Intent intent) {

return new MyBinder();
}
}
在 AndroidManifest.xml 中注册隐式意图的Service:

<service android:name=".service.AidlServerService">
<intent-filter>
<action android:name="cn.zzw.PERSON.SERVER"></action>
</intent-filter>
</service>
至此Server端的实现就算完成了,接下来就是客户端如何调用了。

 

客户端:

1. 在客户端中实现服务端中的第 1,2,3 步。

注意:文件所在的包名一定要和服务端一样!!!

2. 绑定远程服务,获取服务对象。

private void bindToClientService() {
intent = new Intent();
intent.setAction("cn.zzw.PERSON.SERVER");
intent.setPackage("cn.zzw.aidlserverdemo");
bindService(intent,conn,BIND_AUTO_CREATE);
}


ServiceConnection conn =new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mService= IPerson.Stub.asInterface(service);
Log.d(TAG,"Service Connect Successfully");
}

@Override
public void onServiceDisconnected(ComponentName name) {
Log.d(TAG,"Service Connect Fail");
}
};
3.  调用远程服务中的方法

public void onClick(View v) {
switch (v.getId()) {
case R.id.mBtn_hello:
if(mService!=null)
{
String msgInfo = null;
try {
msgInfo = mService.sayHello(mInfo);
Toast.makeText(this,msgInfo,Toast.LENGTH_SHORT).show();
} catch (RemoteException e) {
e.printStackTrace();
}
}else {
Log.d(TAG,"Service is Null");
bindToClientService(http://www.amjmh.com/v/);
}
break;
case R.id.mBtn_calculate:
if(mService!=null)
{

String msgInfo = null;
try {
int sumInfo = mService.sumnNum(50,50);
Toast.makeText(this,""+sumInfo,Toast.LENGTH_SHORT).show();
} catch (RemoteException e) {
e.printStackTrace();
}
}else {
Log.d(TAG,"Service is Null");
bindToClientService();
}
break;
}
}
效果:

先尝试只安装Client,会发现绑定不了Service:

 

log如下:

D/Client: Service is Null
D/Client: Service is Null

posted on 2019-09-12 17:26  激流勇进1  阅读(177)  评论(0编辑  收藏  举报