我这里案例是 通过 IPC 传递对象 (以DemoBean类为例)
- 如下:
AIDL 使用一种简单语法,允许您通过一个或多个方法(可接收参数和返回值)来声明接口。参数和返回值可为任意类型,甚至是 AIDL 生成的其他接口。
使用详情
aidlapplication2(服务端)中的代码实现
类必须支持 Parcelable 接口。支持 Parcelable 接口很重要,因为 Android 系统能通过该接口将对象分解成可编组至各进程的原语。
public class DemoBean 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;
}
public DemoBean(String name, int age) {
this.name = name;
this.age = age;
}
protected DemoBean(Parcel in) {
name = in.readString();
age = in.readInt();
}
public static final Creator<DemoBean> CREATOR = new Creator<DemoBean>() {
@Override
public DemoBean createFromParcel(Parcel in) {
return new DemoBean(in);
}
@Override
public DemoBean[] newArray(int size) {
return new DemoBean[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(@NonNull Parcel parcel, int i) {
parcel.writeString(name);
parcel.writeInt(age);
}
}
parcelable DemoBean; 导包需要手写,注意这里面是没有提示的!!!
parcelable 的 p是小写
写完方法后别忘了 Rebuild project 重新编译一下项目!!!
// DemoInterface.aidl
package com.example.aidlapplication2;
parcelable DemoBean;
// Declare any non-default types here with import statements
interface DemoInterface {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void setDemoBean(in DemoBean demoBean);
DemoBean getDemoBean();
}
public class DemoService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return binder;
}
private DemoInterface.Stub binder=new DemoInterface.Stub(){
private DemoBean demoBean;
@Override
public void setDemoBean(DemoBean demoBean) throws RemoteException {
Log.e("TAG","测试名字---"+demoBean.getName());
this.demoBean=demoBean;
}
@Override
public DemoBean getDemoBean() throws RemoteException {
return demoBean;
}
};
}
aidlapplication1(客户端)中的代码实现
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:onClick="btn0"
android:text="绑定服务"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:onClick="btn1"
android:text="get"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:onClick="btn2"
android:text="set"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
public class MainActivity extends AppCompatActivity {
private DemoServiceConnection demoServiceConnection;
private DemoInterface demoInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private class DemoServiceConnection implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
demoInterface=DemoInterface.Stub.asInterface(iBinder);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
}
//点击绑定服务
public void btn0(View view){
Intent intent=new Intent();
intent.setAction("com.example.aidlapplication2.DemoService");
intent.setPackage("com.example.aidlapplication2");//包名
demoServiceConnection=new DemoServiceConnection();
bindService(intent,demoServiceConnection,BIND_AUTO_CREATE);//绑定服务
}
//点击获取数据
public void btn1(View view){
try {
DemoBean demoBean = demoInterface.getDemoBean();
Log.e("TAG","get---"+demoBean.getName());
} catch (RemoteException e) {
throw new RuntimeException(e);
}
}
//点击写入数据
public void btn2(View view){
try {
demoInterface.setDemoBean(new DemoBean("花花",6));
} catch (RemoteException e) {
throw new RuntimeException(e);
}
}
}
原文地址:https://blog.csdn.net/afufufufu/article/details/131723540
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_47414.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。