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

相关文章

中文分词jieba库的使用与实景应用(一)

知识星球:https://articles.zsxq.com/id_fxvgc803qmr2.html 目录 一.定义: 精确模式(默认模式): 全模式: 搜索引擎模式: paddle 模式(基于深度学习的分词模式): 二 自定义词典 三.文本解析   调整词出现的频率 四. 关键词提取 A. 基于TF-IDF算法的关键词提取 B. 基于TextRank算法的关键词提取

使用SecondaryNameNode恢复NameNode的数据

1)需求: NameNode进程挂了并且存储的数据也丢失了,如何恢复NameNode 此种方式恢复的数据可能存在小部分数据的丢失。 2)故障模拟 (1)kill -9 NameNode进程 [lytfly@hadoop102 current]$ kill -9 19886 (2)删除NameNode存储的数据(/opt/module/hadoop-3.1.4/data/tmp/dfs/na

Hadoop数据压缩使用介绍

一、压缩原则 (1)运算密集型的Job,少用压缩 (2)IO密集型的Job,多用压缩 二、压缩算法比较 三、压缩位置选择 四、压缩参数配置 1)为了支持多种压缩/解压缩算法,Hadoop引入了编码/解码器 2)要在Hadoop中启用压缩,可以配置如下参数

Makefile简明使用教程

文章目录 规则makefile文件的基本语法:加在命令前的特殊符号:.PHONY伪目标: Makefilev1 直观写法v2 加上中间过程v3 伪目标v4 变量 make 选项-f-n-C Make 是一种流行的构建工具,常用于将源代码转换成可执行文件或者其他形式的输出文件(如库文件、文档等)。Make 可以自动化地执行编译、链接等一系列操作。 规则 makefile文件

使用opencv优化图片(画面变清晰)

文章目录 需求影响照片清晰度的因素 实现降噪测试代码 锐化空间锐化Unsharp Masking频率域锐化对比测试 对比度增强常用算法对比测试 需求 对图像进行优化,使其看起来更清晰,同时保持尺寸不变,通常涉及到图像处理技术如锐化、降噪、对比度增强等 影响照片清晰度的因素 影响照片清晰度的因素有很多,主要可以从以下几个方面来分析 1. 拍摄设备 相机传感器:相机传

基本知识点

1、c++的输入加上ios::sync_with_stdio(false);  等价于 c的输入,读取速度会加快(但是在字符串的题里面和容易出现问题) 2、lower_bound()和upper_bound() iterator lower_bound( const key_type &key ): 返回一个迭代器,指向键值>= key的第一个元素。 iterator upper_bou

pdfmake生成pdf的使用

实际项目中有时会有根据填写的表单数据或者其他格式的数据,将数据自动填充到pdf文件中根据固定模板生成pdf文件的需求 文章目录 利用pdfmake生成pdf文件1.下载安装pdfmake第三方包2.封装生成pdf文件的共用配置3.生成pdf文件的文件模板内容4.调用方法生成pdf 利用pdfmake生成pdf文件 1.下载安装pdfmake第三方包 npm i pdfma

零基础学习Redis(10) -- zset类型命令使用

zset是有序集合,内部除了存储元素外,还会存储一个score,存储在zset中的元素会按照score的大小升序排列,不同元素的score可以重复,score相同的元素会按照元素的字典序排列。 1. zset常用命令 1.1 zadd  zadd key [NX | XX] [GT | LT]   [CH] [INCR] score member [score member ...]

git使用的说明总结

Git使用说明 下载安装(下载地址) macOS: Git - Downloading macOS Windows: Git - Downloading Windows Linux/Unix: Git (git-scm.com) 创建新仓库 本地创建新仓库:创建新文件夹,进入文件夹目录,执行指令 git init ,用以创建新的git 克隆仓库 执行指令用以创建一个本地仓库的

【IPV6从入门到起飞】5-1 IPV6+Home Assistant(搭建基本环境)

【IPV6从入门到起飞】5-1 IPV6+Home Assistant #搭建基本环境 1 背景2 docker下载 hass3 创建容器4 浏览器访问 hass5 手机APP远程访问hass6 更多玩法 1 背景 既然电脑可以IPV6入站,手机流量可以访问IPV6网络的服务,为什么不在电脑搭建Home Assistant(hass),来控制你的设备呢?@智能家居 @万物互联