概述
一。以下服务端代码
1.IMyAidlParParcelable.aidl
// IMyAidlParParcelable.aidl
package com.example.zb.adiltestdemo;
import com.example.zb.adiltestdemo.Person;
//自定义类型
interface IMyAidlParParcelable {
List<Person> add(in Person person);
}
2.Person.aidl
package com.example.zb.adiltestdemo;
parcelable Person;
3.Person
package com.example.zb.adiltestdemo;
import android.os.Parcel;
import android.os.Parcelable;
/**
* Created by zb on 2019/4/30.
*/
public class Person implements Parcelable{
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public Person(Parcel source) {
//这里顺序不能乱,一定先name,再age。
this.name=source.readString();
this.age=source.readInt();
}
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;
}
@Override
public int describeContents() {
//描述,不用管它
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(age);
}
public static final Creator<Person> CREATOR=new Creator<Person>() {
@Override
public Person createFromParcel(Parcel source) {
return new Person(source);
}
@Override
public Person[] newArray(int size) {
return new Person[size];
}
};
}
4.服务
public class MyService extends Service {
public MyService() {
}
ArrayList<Person> personlist;
/**
* 当客户端绑定到该服务的时候,会执行该方法
* @param intent
* @return
*/
@Override
public IBinder onBind(Intent intent) {
personlist = new ArrayList<>();
return mIBinder;
}
private IBinder mIBinder=new IMyAidlParParcelable.Stub() {
@Override
public List<Person> add(Person person) throws RemoteException {
personlist.add(person);
return personlist;
}
};
}
二。客户端调用
package com.example.aidlclient;
import android.content.ComponentName;
import android.content.Context;
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.util.Log;
import android.widget.Button;
import android.widget.EditText;
import com.example.zb.adiltestdemo.IMyAidlParParcelable;
import com.example.zb.adiltestdemo.Person;
import java.util.ArrayList;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class MainActivity extends AppCompatActivity {
@BindView(R.id.bt_jisuan)
Button mBtJisuan;
IMyAidlParParcelable iMyAidlParParcelable;
private ServiceConnection conn=new ServiceConnection() {
//绑定服务的时候
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//拿到了远程的服务
iMyAidlParParcelable = IMyAidlParParcelable.Stub.asInterface(service);
}
//断开服务的时候
@Override
public void onServiceDisconnected(ComponentName name) {
//回收资源
iMyAidlParParcelable=null;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
//软件一启动就绑定服务
bindService();
}
@OnClick(R.id.bt_jisuan)
public void onViewClicked() {
try {
ArrayList<Person> persons = (ArrayList<Person>) iMyAidlParParcelable.add(new Person("张三", 19));
Log.i("persons--------",persons.toString());
} catch (RemoteException e) {
e.printStackTrace();
}
}
private void bindService() {
//获取到服务端
Intent intent = new Intent();
//新版本 必须显示intent启动 绑定服务
intent.setComponent(new ComponentName("com.example.zb.adiltestdemo","com.example.zb.adiltestdemo.MyService"));
bindService(intent,conn, Context.BIND_AUTO_CREATE);
}
@Override
protected void onDestroy() {
super.onDestroy();
//解除绑定服务
unbindService(conn);
}
}
注意:在客户端一定要将服务端的IMyAidlParParcelable.aidl
Person.aidl
Person 复制过去,包名一定一定要一致。
github地址:https://github.com/b888zhang/ADILTestDemo
最后
以上就是满意帅哥为你收集整理的AIDL(四)数据传递--自定义类型的全部内容,希望文章能够帮你解决AIDL(四)数据传递--自定义类型所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复