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

相关文章

postgresql使用UUID函数的方法

《postgresql使用UUID函数的方法》本文给大家介绍postgresql使用UUID函数的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录PostgreSQL有两种生成uuid的方法。可以先通过sql查看是否已安装扩展函数,和可以安装的扩展函数

如何使用Lombok进行spring 注入

《如何使用Lombok进行spring注入》本文介绍如何用Lombok简化Spring注入,推荐优先使用setter注入,通过注解自动生成getter/setter及构造器,减少冗余代码,提升开发效... Lombok为了开发环境简化代码,好处不用多说。spring 注入方式为2种,构造器注入和setter

MySQL中比较运算符的具体使用

《MySQL中比较运算符的具体使用》本文介绍了SQL中常用的符号类型和非符号类型运算符,符号类型运算符包括等于(=)、安全等于(=)、不等于(/!=)、大小比较(,=,,=)等,感兴趣的可以了解一下... 目录符号类型运算符1. 等于运算符=2. 安全等于运算符<=>3. 不等于运算符<>或!=4. 小于运

使用zip4j实现Java中的ZIP文件加密压缩的操作方法

《使用zip4j实现Java中的ZIP文件加密压缩的操作方法》本文介绍如何通过Maven集成zip4j1.3.2库创建带密码保护的ZIP文件,涵盖依赖配置、代码示例及加密原理,确保数据安全性,感兴趣的... 目录1. zip4j库介绍和版本1.1 zip4j库概述1.2 zip4j的版本演变1.3 zip4

Python 字典 (Dictionary)使用详解

《Python字典(Dictionary)使用详解》字典是python中最重要,最常用的数据结构之一,它提供了高效的键值对存储和查找能力,:本文主要介绍Python字典(Dictionary)... 目录字典1.基本特性2.创建字典3.访问元素4.修改字典5.删除元素6.字典遍历7.字典的高级特性默认字典

使用Python构建一个高效的日志处理系统

《使用Python构建一个高效的日志处理系统》这篇文章主要为大家详细讲解了如何使用Python开发一个专业的日志分析工具,能够自动化处理、分析和可视化各类日志文件,大幅提升运维效率,需要的可以了解下... 目录环境准备工具功能概述完整代码实现代码深度解析1. 类设计与初始化2. 日志解析核心逻辑3. 文件处

一文详解如何使用Java获取PDF页面信息

《一文详解如何使用Java获取PDF页面信息》了解PDF页面属性是我们在处理文档、内容提取、打印设置或页面重组等任务时不可或缺的一环,下面我们就来看看如何使用Java语言获取这些信息吧... 目录引言一、安装和引入PDF处理库引入依赖二、获取 PDF 页数三、获取页面尺寸(宽高)四、获取页面旋转角度五、判断

C++中assign函数的使用

《C++中assign函数的使用》在C++标准模板库中,std::list等容器都提供了assign成员函数,它比操作符更灵活,支持多种初始化方式,下面就来介绍一下assign的用法,具有一定的参考价... 目录​1.assign的基本功能​​语法​2. 具体用法示例​​​(1) 填充n个相同值​​(2)

MySql基本查询之表的增删查改+聚合函数案例详解

《MySql基本查询之表的增删查改+聚合函数案例详解》本文详解SQL的CURD操作INSERT用于数据插入(单行/多行及冲突处理),SELECT实现数据检索(列选择、条件过滤、排序分页),UPDATE... 目录一、Create1.1 单行数据 + 全列插入1.2 多行数据 + 指定列插入1.3 插入否则更

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命