当前位置: 首页 > article >正文

Android 多进程开发 - FileDescriptor、Uri、AIDL 接口定义不能抛出异常

FileDescriptor1、AIDLIMyAidlInterface.aidl这里是位于src/main/java/com/my/common包下packagecom.my.common;importandroid.os.ParcelFileDescriptor;interfaceIMyAidlInterface{ParcelFileDescriptorgetFileDescriptor();voidsetFileDescriptor(inParcelFileDescriptorparcelFileDescriptor);}2、ServerprivatefinalIMyAidlInterface.StubbindernewIMyAidlInterface.Stub(){OverridepublicParcelFileDescriptorgetFileDescriptor()throwsRemoteException{FilefilesDirgetFilesDir();FilefilenewFile(filesDir,shared_file.dat);// 如果示例文件不存在则创建并写入一些数据if(!file.exists()){try{file.createNewFile();}catch(IOExceptione){e.printStackTrace();}try{FileOutputStreamfileOutputStreamnewFileOutputStream(file);fileOutputStream.write(server content.getBytes());fileOutputStream.close();}catch(IOExceptione){e.printStackTrace();}}try{returnParcelFileDescriptor.open(file,ParcelFileDescriptor.MODE_READ_ONLY);}catch(FileNotFoundExceptione){e.printStackTrace();returnnull;}}OverridepublicvoidsetFileDescriptor(ParcelFileDescriptorparcelFileDescriptor)throwsRemoteException{if(parcelFileDescriptor!null){try(FileInputStreamfileInputStreamnewFileInputStream(parcelFileDescriptor.getFileDescriptor())){byte[]contentnewbyte[fileInputStream.available()];intreadfileInputStream.read(content);Log.i(TAG,read read bytes from file);Log.i(TAG,file content: newString(content));}catch(IOExceptione){Log.i(TAG,open file error: e.getMessage());}finally{try{parcelFileDescriptor.close();}catch(IOExceptione){e.printStackTrace();}}}}};3、Client调用 getFileDescriptor 方法try{ParcelFileDescriptorparcelFileDescriptormyAidlInterface.getFileDescriptor();Log.i(TAG,getFileDescriptor method success);if(parcelFileDescriptor!null){try(FileInputStreamfileInputStreamnewFileInputStream(parcelFileDescriptor.getFileDescriptor())){byte[]contentnewbyte[fileInputStream.available()];intreadfileInputStream.read(content);Log.i(TAG,read read bytes from file);Log.i(TAG,file content: newString(content));}catch(IOExceptione){Log.i(TAG,open file error: e.getMessage());}finally{try{parcelFileDescriptor.close();}catch(IOExceptione){e.printStackTrace();}}}}catch(RemoteExceptione){e.printStackTrace();Log.e(TAG,getFileDescriptor method error: e.getMessage());}调用 setFileDescriptor 方法FilefilesDirgetFilesDir();FilefilenewFile(filesDir,shared_file.dat);// 如果示例文件不存在则创建并写入一些数据if(!file.exists()){try{file.createNewFile();}catch(IOExceptione){e.printStackTrace();}try{FileOutputStreamfileOutputStreamnewFileOutputStream(file);fileOutputStream.write(client content.getBytes());fileOutputStream.close();}catch(IOExceptione){e.printStackTrace();}}try{ParcelFileDescriptorparcelFileDescriptorParcelFileDescriptor.open(file,ParcelFileDescriptor.MODE_READ_ONLY);try{myAidlInterface.setFileDescriptor(parcelFileDescriptor);Log.i(TAG,setFileDescriptor method success);}catch(RemoteExceptione){e.printStackTrace();Log.e(TAG,setFileDescriptor method error: e.getMessage());}}catch(FileNotFoundExceptione){e.printStackTrace();}4、Test调用 getFileDescriptor 方法输出结果如下getFileDescriptor method success read 14 bytes from file file content: server content调用 setFileDescriptor 方法输出结果如下read 14 bytes from file file content: client content setFileDescriptor method successUri1、AIDLIMyAidlInterface.aidl这里是位于src/main/java/com/my/common包下packagecom.my.common;importandroid.net.Uri;interfaceIMyAidlInterface{UrigetUri();voidsetUri(inUriuri);}2、ServerprivatefinalIMyAidlInterface.StubbindernewIMyAidlInterface.Stub(){OverridepublicUrigetUri()throwsRemoteException{FilefilesDirgetFilesDir();FilefilenewFile(filesDir,shared_file.dat);// 如果示例文件不存在则创建并写入一些数据if(!file.exists()){try{file.createNewFile();}catch(IOExceptione){e.printStackTrace();}try{FileOutputStreamfileOutputStreamnewFileOutputStream(file);fileOutputStream.write(server content.getBytes());fileOutputStream.close();}catch(IOExceptione){e.printStackTrace();}}UriuriFileProvider.getUriForFile(ServerService.this,getApplicationContext().getPackageName().fileprovider,file);// 授予临时权限// 第一个参数是客户端应用包名grantUriPermission(com.my.client,uri,Intent.FLAG_GRANT_READ_URI_PERMISSION);returnuri;}OverridepublicvoidsetUri(Uriuri)throwsRemoteException{if(uri!null){try(InputStreaminputStreamgetContentResolver().openInputStream(uri)){byte[]contentnewbyte[inputStream.available()];intreadinputStream.read(content);Log.i(TAG,read read bytes from uri);Log.i(TAG,uri content: newString(content));}catch(IOExceptione){e.printStackTrace();}}}};3、Client调用 getUri 方法try{UriurimyAidlInterface.getUri();Log.i(TAG,getUri method success);if(uri!null){try(InputStreaminputStreamgetContentResolver().openInputStream(uri)){byte[]contentnewbyte[inputStream.available()];intreadinputStream.read(content);Log.i(TAG,read read bytes from uri);Log.i(TAG,uri content: newString(content));}catch(IOExceptione){e.printStackTrace();}}}catch(RemoteExceptione){e.printStackTrace();Log.e(TAG,getUri method error: e.getMessage());}调用 setUri 方法FilefilesDirgetFilesDir();FilefilenewFile(filesDir,shared_file.dat);// 如果示例文件不存在则创建并写入一些数据if(!file.exists()){try{file.createNewFile();}catch(IOExceptione){e.printStackTrace();}try{FileOutputStreamfileOutputStreamnewFileOutputStream(file);fileOutputStream.write(client content.getBytes());fileOutputStream.close();}catch(IOExceptione){e.printStackTrace();}}UriuriFileProvider.getUriForFile(this,getPackageName().fileprovider,file);grantUriPermission(com.my.server,uri,Intent.FLAG_GRANT_READ_URI_PERMISSION);try{myAidlInterface.setUri(uri);Log.i(TAG,setUri method success);}catch(RemoteExceptione){e.printStackTrace();Log.e(TAG,setUri method error: e.getMessage());}4、Test调用 getUri 方法输出结果如下getUri method success read 14 bytes from uri uri content: server content调用 setUri 方法输出结果如下read 14 bytes from uri uri content: client content setUri method successAIDL 接口定义不能抛出异常在 AIDL 接口定义中方法签名本身不能包含 throws 关键字来声明抛出异常会导致编译错误intadd(inta,intb)throwsRemoteException;# 输出结果 syntax error, unexpected identifier, expecting or ;onewayvoiddoSomething()throwsRemoteException;# 输出结果 syntax error, unexpected identifier, expecting or ;将光标移动到 throws 关键字上会出现如下提示信息AidlTokenTypes. ASSIGN expected, got throws

相关文章:

Android 多进程开发 - FileDescriptor、Uri、AIDL 接口定义不能抛出异常

FileDescriptor 1、AIDL IMyAidlInterface.aidl,这里是位于 src/main/java/com/my/common 包下 package com.my.common;import android.os.ParcelFileDescriptor;interface IMyAidlInterface {ParcelFileDescriptor getFileDescriptor();void setFileDescriptor(in …...

KMP算法详解 [c++]

目录 前言 朴素的模式匹配算法 KMP模式匹配算法 KMP模式匹配算法的原理 next数组值的推导 KMP模式匹配算法的实现 KMP模式匹配算法的改进 nextval的推导 优化后的KMP模式匹配算法代码 零、前言 每年新闻周刊都会发布年度十大热词,这其实查询某个字符串在其…...

AD7685的SPI接口调试过程(附完整代码)

该系列的ADC主要差别是在转换速率上,AD7685的最大转换速率是250kSPS。我们主要是看芯片SPI接口和主机的通信:单个ADC和兼容SPI接口的主机通信时,一般会用三线且无繁忙指示模式,该模式的时序图如下所示:主要注意以下几点…...

L298N 直流电机驱动模块与 Arduino 的接口

虽然您最终需要学习控制直流电机才能构建自己的机器人,但您可能需要一些更容易上手的东西 - 这就是 L298N 电机驱动器的用武之地。它可以控制速度和旋转两个直流电机的方向。此外,它还可以 控制直流电机 只有能够控制直流电机的速度和旋转方向,我们才能完全控制它。通过结…...

【LLM infra】Megatron-LM | deepspeed | 量化/推理框架

note LLM推理过程: prefill:每层都得到历史token的kv cache,最后一个位置输出 logits;decode:对刚才新生成的token,计算它的Q/K/V,用它的 Q 去 attend 历史所有 K/V cache,输出下一…...

mimic数据库提取小问题解决

sql学艺不精,所以基本上自己开发一套“专属sql”后后面都是套用。首先是拼接问题,正常提取出目标人群后,需要不断拼接demo,treat,lab等数据,像demo,treat这些可能还好,但lab这些短时…...

推荐:Jib — 容器化你的Java应用的新选择!

推荐:Jib — 容器化你的Java应用的新选择! 【免费下载链接】jib GoogleContainerTools/jib: 是一个基于 Java 的 Docker 镜像构建工具,支持多种容器镜像构建选项和插件。该项目提供了一个简单易用的 Docker 镜像构建工具,可以方便…...

好用的软件、网站、插件记录

好的科研工具能够帮助我们事半功倍,华而不实的工具浪费我们的时间。在此记录一下,一些好用且目前我长期在使用的工具。 目的:希望帮助刚入门的同学快速上手,节省找工具的时间,将更多的时间投入自己的科研任务。 好用的…...

【亲测免费】 探索知识图谱的力量: rahulnyk/knowledge_graph 项目详解

探索知识图谱的力量: rahulnyk/knowledge_graph 项目详解 【免费下载链接】knowledge_graph Convert any text to a graph of knowledge. This can be used for Graph Augmented Generation or Knowledge Graph based QnA 项目地址: https://gitcode.com/gh_mirro…...

贡献者必读:如何参与SIMP开源项目并提交高质量代码

贡献者必读:如何参与SIMP开源项目并提交高质量代码 【免费下载链接】SIMP A system automation and configuration management stack targeted toward operational flexibility and policy compliance. 项目地址: https://gitcode.com/gh_mirrors/si/SIMP SI…...

【数字信号调制】基于8相移键控8-PSK调制数字通信系统(含模拟噪声信道上的信号传输,包括调制、噪声添加、解调以及符号和比特错误率的性能评估)附Matlab代码

✅作者简介:热爱科研的Matlab仿真开发者,擅长数据处理、建模仿真、程序设计、完整代码获取、论文复现及科研仿真。🍎 往期回顾关注个人主页:Matlab科研工作室🍊个人信条:格物致知,完整Matlab代码及仿真咨询…...

【状态估计】基于卡尔曼滤波器实现月球陨石坑导航附Matlab代码

✅作者简介:热爱科研的Matlab仿真开发者,擅长数据处理、建模仿真、程序设计、完整代码获取、论文复现及科研仿真。🍎 往期回顾关注个人主页:Matlab科研工作室🍊个人信条:格物致知,完整Matlab代码及仿真咨询…...

【雷达干扰】基于CFastICA交叉极化干扰对消-独立成分分析附Matlab代码

✅作者简介:热爱科研的Matlab仿真开发者,擅长数据处理、建模仿真、程序设计、完整代码获取、论文复现及科研仿真。🍎 往期回顾关注个人主页:Matlab科研工作室🍊个人信条:格物致知,完整Matlab代码及仿真咨询…...

Windows 10 RS2及以上系统中使用TDL的注意事项:IRQL级别变化与驱动开发调整

Windows 10 RS2及以上系统中使用TDL的注意事项:IRQL级别变化与驱动开发调整 【免费下载链接】TDL Driver loader for bypassing Windows x64 Driver Signature Enforcement 项目地址: https://gitcode.com/gh_mirrors/tdl1/TDL TDL(Turla Driver …...

TDL驱动开发实战:如何编写适用于Turla Driver Loader的“无驱动”内核模块

TDL驱动开发实战:如何编写适用于Turla Driver Loader的“无驱动”内核模块 【免费下载链接】TDL Driver loader for bypassing Windows x64 Driver Signature Enforcement 项目地址: https://gitcode.com/gh_mirrors/tdl1/TDL TDL(Turla Driver L…...

光纤熔接实验教程

一、实验目的掌握光纤熔接的基本原理和操作步骤。学习使用光纤熔接机进行光纤的精确对接。了解光纤熔接的质量评估方法。二、实验设备与工具光纤熔接机:用于光纤的精确对准和熔接。光纤切割刀:用于切割光纤端面,确保端面平整。光纤剥线钳&…...

QgraphicsView异步线程加载地图瓦片

本节主要记录一下qt开发过程中离线地图瓦片的加载方式,瓦片加载选择graphicsView控件,同时为了不影响主线程事件和其他操作,这里采用了异步线程的操作,将地图瓦片加载的步骤放到了异步子线程之中。注:本记录仅为本人笔…...

Linux课程六课---Linux进程控制

作者前言 🎂 ✨✨✨✨✨✨🍧🍧🍧🍧🍧🍧🍧🎂 ​🎂 作者介绍: 🎂🎂 🎂 🎉🎉&#x1f389…...

华为OD机试双机位C卷-乘坐保密电梯 (Py/Java/C/C++/Js/Go)

乘坐保密电梯 华为OD机试双机位C卷 华为OD上机考试双机位C卷 100分题型 华为OD机试双机位C卷真题目录点击查看: 华为OD机试双机位C卷真题题库目录|机考题库 + 算法考点详解 题目描述 有一座保密大楼,你从0楼到达指定楼层m,必须这样的规则乘坐电梯: 给定一个数字序列,每…...

double-conversion开发者必备:测试用例编写与验证完全指南

double-conversion开发者必备:测试用例编写与验证完全指南 【免费下载链接】double-conversion Efficient binary-decimal and decimal-binary conversion routines for IEEE doubles. 项目地址: https://gitcode.com/gh_mirrors/do/double-conversion doubl…...

Gatt社区贡献指南:如何参与开源项目并提交PR

Gatt社区贡献指南:如何参与开源项目并提交PR 【免费下载链接】gatt Gatt is a Go package for building Bluetooth Low Energy peripherals 项目地址: https://gitcode.com/gh_mirrors/ga/gatt Gatt是一个用于构建蓝牙低功耗(BLE)外设…...

如何通过eluceo iCal 2创建重复事件与例外日期?

如何通过eluceo iCal 2创建重复事件与例外日期? 【免费下载链接】iCal iCal-creator for PHP 项目地址: https://gitcode.com/gh_mirrors/ic/iCal eluceo iCal 2是一款强大的PHP iCal创建工具,能够帮助开发者轻松生成符合iCalendar标准的日历文件…...

如何将Bramses‘ Highly Opinionated Vault 2023与外部工具集成:Readwise、Alfred与Zotero协同工作流

如何将Bramses Highly Opinionated Vault 2023与外部工具集成:Readwise、Alfred与Zotero协同工作流 【免费下载链接】bramses-highly-opinionated-vault-2023 A highly opinionated, fully featured Obsidian vault that can get you from Zero to Zettelkasten lic…...

TextAttack实战教程:用5行代码实现BERT模型的对抗性攻击

TextAttack实战教程:用5行代码实现BERT模型的对抗性攻击 【免费下载链接】TextAttack TextAttack 🐙 is a Python framework for adversarial attacks, data augmentation, and model training in NLP https://textattack.readthedocs.io/en/master/ 项…...

打造响应式导航:laravel-menu与Bootstrap 3/5的完美结合方案

打造响应式导航:laravel-menu与Bootstrap 3/5的完美结合方案 【免费下载链接】laravel-menu A quick way to create menus in Laravel 项目地址: https://gitcode.com/gh_mirrors/la/laravel-menu laravel-menu是一款专为Laravel框架设计的菜单创建工具&…...

Matic Network合约升级机制解析:Proxy模式与Governance如何确保系统可扩展性

Matic Network合约升级机制解析:Proxy模式与Governance如何确保系统可扩展性 【免费下载链接】contracts Smart contracts comprising the business logic of the Matic Network 项目地址: https://gitcode.com/gh_mirrors/con/contracts Matic Network作为高…...

Clojure开发者的Python之旅:从语法差异到实战技巧

Clojure开发者的Python之旅:从语法差异到实战技巧 【免费下载链接】libpython-clj Python bindings for Clojure 项目地址: https://gitcode.com/gh_mirrors/li/libpython-clj 作为一名Clojure开发者,当你需要与Python生态系统交互时,…...

DuckieTV自定义界面教程:从主题切换到快捷键设置,打造你的专属追剧工具

DuckieTV自定义界面教程:从主题切换到快捷键设置,打造你的专属追剧工具 【免费下载链接】DuckieTV A web application built with AngularJS to track your favorite tv-shows with semi-automagic torrent integration 项目地址: https://gitcode.com…...

dbblog常见问题解决:从安装到运行的15个实用技巧

dbblog常见问题解决:从安装到运行的15个实用技巧 【免费下载链接】dbblog 基于SpringBoot2.xVue2.xElementUIIviewElasticsearchRabbitMQRedisShiro的多模块前后端分离的博客项目 项目地址: https://gitcode.com/gh_mirrors/db/dbblog dbblog是一个基于Sprin…...

eslint-plugin-jest完全指南:如何用ESLint提升Jest测试代码质量

eslint-plugin-jest完全指南:如何用ESLint提升Jest测试代码质量 【免费下载链接】eslint-plugin-jest ESLint plugin for Jest 项目地址: https://gitcode.com/gh_mirrors/es/eslint-plugin-jest eslint-plugin-jest是一款专为Jest测试框架设计的ESLint插件…...