AIDL基本使用3—-in out inout的用

2024-03-10 19:59
文章标签 使用 基本 aidl inout

本文主要是介绍AIDL基本使用3—-in out inout的用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在AIDL中客户端和服务端传入参数 是可以设置流向.仅限参数不包含返回值
1. in :客户端可以传入参数到服务到(默认方法)
2. out:服务端修改客户端传入参数对象 会影响客户端的传入实例
3. inout:服务端即可接受客户端参数也可以修改对其客户端实例影响

这个标签在哪?


这里用AIDL基本使用2的Demo作为案例:AIDL基本使用2

在AIDL基本使用2案例中 IMyAidlInterface.aidl 用来作为客户端和服务端交互的接口.

// IMyAidlInterface.aidl
package com.ucoupon.myservice;
import com.ucoupon.myservice2.Book;interface IMyAidlInterface {String bookIn(in Book mbook);String bookOut(out Book mbook);String bookInout(inout Book mbook);
}

看上面的代码中可以知道.in out inout是用来修饰aidl接口中传入参数.

我们在复习下Book.java 里面有什么

package com.ucoupon.myservice2;import android.os.Parcel;
import android.os.Parcelable;/*** Created by FMY on 2017/5/18.*/public class Book implements Parcelable {String name;int id;public Book(String name, int id) {this.name = name;this.id = id;}protected Book(Parcel in) {name = in.readString();id = in.readInt();}public static final Creator<Book> CREATOR = new Creator<Book>() {@Overridepublic Book createFromParcel(Parcel in) {return new Book(in);}@Overridepublic Book[] newArray(int size) {return new Book[size];}};@Overridepublic int describeContents() {return 0;}@Overridepublic void writeToParcel(Parcel dest, int flags) {dest.writeString(name);dest.writeInt(id);}
}

一个很普通的类有两个字段

  • 来编译看看
    编译出错,出错于自动生成接口java文件.new Book()无法被实例化.
    因为根本没有这个构造方法.
    这里写图片描述

为什么我们在AIDL基本使用的2中没有报错?
继续查看源码

这里写图片描述

如果被修饰方法参数为out那么会自动创建修饰参数的空构造方法
在本例中有如下方法被out修饰:
String bookOut(out Book mbook);

解决办法:创建一个空构造方法

  • 继续编译
    编译又报错
    这里写图片描述

可以看到Book实例化调用readFromParcel方法.可是book没有这个方法.

解决办法:
在Book类中添加此方法.此方法是用于客户端用了out或者inout修饰的方法 中读取服务端修改后的对象的数值在赋值给客户端

package com.ucoupon.myservice2;import android.os.Parcel;
import android.os.Parcelable;/*** Created by FMY on 2017/5/18.*/public class Book implements Parcelable {....public void readFromParcel(Parcel reply) {name = reply.readString();id = reply.readInt();}.....
}

修改后编译通过;

然后把文件拷贝到客户端 进行绑定操作 这一步略过


in测试

来看服务段代码

package com.ucoupon.myservice;import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.util.Log;import com.ucoupon.myservice2.Book;public class MyService extends Service {public MyService() {}@Overridepublic void onCreate() {super.onCreate();Log.d(TAG, "onCreate() called");}//绑定的时候回调@Nullable@Overridepublic IBinder onBind(Intent intent) {Log.d(TAG, "onBind() called with: intent = [" + intent + "]");return new MyAidlInterface();}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.d(TAG, "onStartCommand() called with: intent = [" + intent + "], flags = [" + flags + "], startId = [" + startId + "]");return super.onStartCommand(intent, flags, startId);}@Overridepublic void unbindService(ServiceConnection conn) {Log.d(TAG, "unbindService() called with: conn = [" + conn + "]");super.unbindService(conn);}@Overridepublic void onRebind(Intent intent) {Log.d(TAG, "onRebind() called with: intent = [" + intent + "]");super.onRebind(intent);}@Overridepublic boolean onUnbind(Intent intent) {Log.d(TAG, "onUnbind() called with: intent = [" + intent + "]");return super.onUnbind(intent);}@Overridepublic void unregisterReceiver(BroadcastReceiver receiver) {Log.d(TAG, "unregisterReceiver() called with: receiver = [" + receiver + "]");super.unregisterReceiver(receiver);}@Overridepublic void onDestroy() {super.onDestroy();Log.d(TAG, "onDestroy() called");}@Overridepublic void onStart(Intent intent, int startId) {Log.d(TAG, "onStart() called with: intent = [" + intent + "], startId = [" + startId + "]");super.onStart(intent, startId);}private static final String TAG = "MyService";class MyAidlInterface extends IMyAidlInterface.Stub{@Overridepublic String bookIn(Book mbook) throws RemoteException {Log.d(TAG, "bookIn() called with: mbook = [" + mbook + "]");mbook.id = 233;return null;}@Overridepublic String bookOut(Book mbook) throws RemoteException {Log.d(TAG, "bookout() called with: mbook = [" + mbook + "]");mbook.id = 233;return null;}@Overridepublic String bookInout(Book mbook) throws RemoteException {Log.d(TAG, "bookInout() called with: mbook = [" + mbook + "]");mbook.id = 233;return null;}}
}

客户端:

package com.ucoupon.aidlstudy;import android.content.ComponentName;
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 com.ucoupon.myservice.IMyAidlInterface;
import com.ucoupon.myservice2.Book;public class MainActivity extends AppCompatActivity {private Myconnect myconnect;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//连接管理myconnect = new Myconnect();//意图Intent intent = new Intent();intent.setClassName("com.ucoupon.myservice","com.ucoupon.myservice.MyService");startService(intent);//开始绑定服务bindService(intent,myconnect,BIND_AUTO_CREATE);}private static final String TAG = "MainActivity";class Myconnect implements ServiceConnection {//连接的成功的时候回调@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {Log.d(TAG, "onServiceConnected() called with: name = [" + name + "], service = [" + service + "]");IMyAidlInterface iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);Book mBook = new Book("客户端", -1);try {Log.e("fmy","客户端没有调用 bookin方法前mBook: name = "+mBook.name+","+" id"+mBook.id);iMyAidlInterface.bookIn(mBook);Log.e("fmy","客户端调用后 bookin方法后mBook: name = "+mBook.name+","+" id"+mBook.id);} catch (RemoteException e) {e.printStackTrace();}//解绑服务
//            unbindService(myconnect);}//断开连接的时候回调@Overridepublic void onServiceDisconnected(ComponentName name) {Log.d(TAG, "onServiceDisconnected() called with: name = [" + name + "]");}}
}

结果预测:
根据前言in 可以让客户端传入一个对象给服务端.但是服务端拿到对象后修改对客户端实例是没有效果

运行结果:
客户端:

E/fmy: 客户端没有调用 bookin方法前mBook: name = 客户端, id-1
客户端调用后 bookin方法后mBook: name = 客户端, id-1

服务端:

 mbook = [Book{name='客户端', id=-1}]

从上面的日志可以发现客户端调用前后book实例是没有任何变化.
服务端也正确读取到客户端发送book实例信息.
回过头来再看看bookIn方法.

  @Overridepublic String bookIn(Book mbook) throws RemoteException {Log.d(TAG, "bookIn() called with: mbook = [" + mbook + "]");mbook.id = 233;return null;}

方法中bookIn对传入的book对象修改了id为233.但是客户端在调用后并有没有影响自身book对象.

out测试

客户端代码和上面in测试差不多,只改变调用bookout方法而已
客户端:

Book mBook = new Book("客户端", -1);try {Log.e("fmy","客户端没有调用 bookout方法前mBook: name = "+mBook.name+","+" id"+mBook.id);iMyAidlInterface.bookOut(mBook);Log.e("fmy","客户端调用后 bookout方法后mBook: name = "+mBook.name+","+" id"+mBook.id);} catch (RemoteException e) {e.printStackTrace();}

预期结果:
out定义:服务端无法读取从客户端传入的参数,但可以改变传入的对象,然后对客户端的实例对象有影响

客户端没有调用 bookout方法前mBook: name = 客户端, id-1
客户端调用后 bookout方法后mBook: name = null, id233

服务端:

mbook = [Book{name='null', id=0}]

判断正确:因为out是客户端无法传入参数给服务端,所以服务端的book参数都为默认值.
这时服务端修改book的id为233.然后把服务端的book传回.所以调用后客户端的name为空 id为233

AIDL基本使用4—-linkToDeath和unlinkToDeath

这篇关于AIDL基本使用3—-in out inout的用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/795351

相关文章

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

SpringBoot中如何使用Assert进行断言校验

《SpringBoot中如何使用Assert进行断言校验》Java提供了内置的assert机制,而Spring框架也提供了更强大的Assert工具类来帮助开发者进行参数校验和状态检查,下... 目录前言一、Java 原生assert简介1.1 使用方式1.2 示例代码1.3 优缺点分析二、Spring Fr

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

Java中的数组与集合基本用法详解

《Java中的数组与集合基本用法详解》本文介绍了Java数组和集合框架的基础知识,数组部分涵盖了一维、二维及多维数组的声明、初始化、访问与遍历方法,以及Arrays类的常用操作,对Java数组与集合相... 目录一、Java数组基础1.1 数组结构概述1.2 一维数组1.2.1 声明与初始化1.2.2 访问

SpringBoot线程池配置使用示例详解

《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected

C++ Log4cpp跨平台日志库的使用小结

《C++Log4cpp跨平台日志库的使用小结》Log4cpp是c++类库,本文详细介绍了C++日志库log4cpp的使用方法,及设置日志输出格式和优先级,具有一定的参考价值,感兴趣的可以了解一下... 目录一、介绍1. log4cpp的日志方式2.设置日志输出的格式3. 设置日志的输出优先级二、Window

Ubuntu如何分配​​未使用的空间

《Ubuntu如何分配​​未使用的空间》Ubuntu磁盘空间不足,实际未分配空间8.2G因LVM卷组名称格式差异(双破折号误写)导致无法扩展,确认正确卷组名后,使用lvextend和resize2fs... 目录1:原因2:操作3:报错5:解决问题:确认卷组名称​6:再次操作7:验证扩展是否成功8:问题已解

Qt使用QSqlDatabase连接MySQL实现增删改查功能

《Qt使用QSqlDatabase连接MySQL实现增删改查功能》这篇文章主要为大家详细介绍了Qt如何使用QSqlDatabase连接MySQL实现增删改查功能,文中的示例代码讲解详细,感兴趣的小伙伴... 目录一、创建数据表二、连接mysql数据库三、封装成一个完整的轻量级 ORM 风格类3.1 表结构

使用Docker构建Python Flask程序的详细教程

《使用Docker构建PythonFlask程序的详细教程》在当今的软件开发领域,容器化技术正变得越来越流行,而Docker无疑是其中的佼佼者,本文我们就来聊聊如何使用Docker构建一个简单的Py... 目录引言一、准备工作二、创建 Flask 应用程序三、创建 dockerfile四、构建 Docker