Android 多进程开发 - 服务端死亡回调、服务端与客户端的线程环境、oneway 关键字
一、服务端死亡回调在绑定服务端时可以获取服务端时的 Binder 对象调用 Binder 对象的 linkToDeath 方法注册死亡回调监听服务端时是否挂了privateIMyAidlInterfacemyAidlInterface;privatebooleanisBoundfalse;privatefinalIBinder.DeathRecipientdeathRecipientnewIBinder.DeathRecipient(){OverridepublicvoidbinderDied(){Log.i(TAG,检测到服务端进程死亡);}};privateServiceConnectionconnectionnewServiceConnection(){OverridepublicvoidonServiceConnected(ComponentNamename,IBinderservice){Log.i(TAG,服务连接成功);myAidlInterfaceIMyAidlInterface.Stub.asInterface(service);try{service.linkToDeath(deathRecipient,0);}catch(RemoteExceptione){e.printStackTrace();}isBoundtrue;}OverridepublicvoidonServiceDisconnected(ComponentNamename){Log.i(TAG,服务连接断开);myAidlInterfacenull;isBoundfalse;// 系统会处理 unlinkToDeath、unbindService}};触发 onServiceDisconnected 回调系统会处理 unlinkToDeath、unbindService不需要手动调用详见源码// 1. doDeath 方法源码publicvoiddoDeath(ComponentNamename,IBinderservice){synchronized(this){ConnectionInfooldmActiveConnections.get(name);if(oldnull||old.binder!service){// Death for someone different than who we last// reported... just ignore it.return;}mActiveConnections.remove(name);old.binder.unlinkToDeath(old.deathMonitor,0);}mConnection.onServiceDisconnected(name);}// doDeath 方法中的清理逻辑mActiveConnections.remove(name);old.binder.unlinkToDeath(old.deathMonitor,0);// 2. doConnected 方法publicvoiddoConnected(ComponentNamename,IBinderservice,booleandead){ServiceDispatcher.ConnectionInfoold;ServiceDispatcher.ConnectionInfoinfo;synchronized(this){if(mForgotten){// We unbound before receiving the connection; ignore// any connection received.return;}oldmActiveConnections.get(name);if(old!nullold.binderservice){// Huh, already have this one. Oh well!return;}if(service!null){// A new service is being connected... set it all up.infonewConnectionInfo();info.binderservice;info.deathMonitornewDeathMonitor(name,service);try{service.linkToDeath(info.deathMonitor,0);mActiveConnections.put(name,info);}catch(RemoteExceptione){// This service was dead before we got it... just// dont do anything with it.mActiveConnections.remove(name);return;}}else{// The named service is being disconnected... clean up.mActiveConnections.remove(name);}if(old!null){old.binder.unlinkToDeath(old.deathMonitor,0);}}// If there was an old service, it is now disconnected.if(old!null){mConnection.onServiceDisconnected(name);}if(dead){mConnection.onBindingDied(name);}else{// If there is a new viable service, it is now connected.if(service!null){mConnection.onServiceConnected(name,service);}else{// The binding machinery worked, but the remote returned null from onBind().mConnection.onNullBinding(name);}}}// doConnected 方法中的清理逻辑if(old!null){old.binder.unlinkToDeath(old.deathMonitor,0);}二、服务端与客户端的线程环境1、服务端AIDL 接口在主线程执行无论客户端是在主线程调用还是在子线程调用privatefinalIMyAidlInterface.StubbindernewIMyAidlInterface.Stub(){Overridepublicintadd(inta,intb)throwsRemoteException{// Binder 线程returnab;}}DeathRecipient 回调在 Binder 线程执行IBinder.DeathRecipientdrnewDeathRecipient(){OverridepublicvoidbinderDied(){// Binder 线程}};2、客户端ServiceConnection 回调在主线程执行privateServiceConnectionconnectionnewServiceConnection(){OverridepublicvoidonServiceConnected(ComponentNamename,IBinderservice){// 主线程}OverridepublicvoidonServiceDisconnected(ComponentNamename){// 主线程}};DeathRecipient 回调在 Binder 线程执行privatefinalIBinder.DeathRecipientdeathRecipientnewIBinder.DeathRecipient(){OverridepublicvoidbinderDied(){// 主线程}};AIDL 接口调用调用前后都是在调用者线程执行try{// 主线程intresultmyAidlInterface.add(5,3);Log.i(TAG,add result: result);// 主线程}catch(RemoteExceptione){e.printStackTrace();Log.e(TAG,add method error: e.getMessage());}newThread(()-{try{// 子线程intresultmyAidlInterface.add(5,3);Log.i(TAG,add result: result);// 子线程}catch(RemoteExceptione){e.printStackTrace();Log.e(TAG,add method error: e.getMessage());}}).start();AIDL 接口回调在主线程执行无论客户端是在主线程注册还是在子线程注册callbacknewIPlayerCallback.Stub(){OverridepublicvoidonSongChanged(StringsongName)throwsRemoteException{// 主线程Log.i(TAG,onSongChanged: songName);}OverridepublicvoidonPlayStateChanged(booleanisPlaying)throwsRemoteException{// 主线程Log.i(TAG,onPlayStateChanged: isPlaying);}};try{myAidlInterface.registerCallback(callback);Log.i(TAG,registerCallback method success);}catch(RemoteExceptione){e.printStackTrace();Log.e(TAG,registerCallback method error: e.getMessage());}newThread(()-{callbacknewIPlayerCallback.Stub(){OverridepublicvoidonSongChanged(StringsongName)throwsRemoteException{// 主线程Log.i(TAG,onSongChanged: songName);}OverridepublicvoidonPlayStateChanged(booleanisPlaying)throwsRemoteException{// 主线程Log.i(TAG,onPlayStateChanged: isPlaying);}};try{myAidlInterface.registerCallback(callback);Log.i(TAG,registerCallback method success);}catch(RemoteExceptione){e.printStackTrace();Log.e(TAG,registerCallback method error: e.getMessage());}}).start();三、oneway 关键字1、基本介绍oneway 关键字修饰的方法客户端调用后立即返回不等待服务端执行完成onewayvoiddoSomething();2、注意事项oneway 方法不能有返回值否则编译报错onewayStringdoSomething();# 输出结果 oneway method doSomething cannot return a valueoneway 方法的参数可以用 in 修饰onewayvoiddoSomething(inbyte[]data);oneway 方法的参数不能用 out 或 inout 修饰否则编译报错onewayvoiddoSomething(outbyte[]data);# 输出结果 oneway method doSomething cannot have out parametersonewayvoiddoSomething(inoutbyte[]data);# 输出结果 oneway method doSomething cannot have out parameters

相关新闻

Vue - Vue2 与 Vue3 自定义插件

Vue - Vue2 与 Vue3 自定义插件

Vue2 与 Vue3 自定义插件实现 在 Vue2 中,插件是一个包含 install 方法的对象,接收 Vue 构造函数作为第一个参数,以及插件选项作为第二个参数 // myPlugin.js const MyPlugin {install(Vue, options) {// 1. 添加全局方法或属性Vue.myGlobal…

2026/6/16 3:19:28 阅读更多 →
一台电脑控制N台手机实现投屏群控操作,搭建引流工作室必备技能

一台电脑控制N台手机实现投屏群控操作,搭建引流工作室必备技能

前言:做过引流获客工作室的老板或同行都知道,通过单个账号引流的风险其实挺大的,一是引流的获客通道太少,引流效果大打折扣。二是随着平台规则越来越严,即使单个账号做得再成功,万一违规就全凉了。这个时候…

2026/7/3 16:25:23 阅读更多 →
100个AI术语表:从核心概念到前沿技术,这份指南助你快速上手大模型时代!

100个AI术语表:从核心概念到前沿技术,这份指南助你快速上手大模型时代!

本文提供了一份AI产品经理必备的100个关键术语表,分为三个层次深入浅出地讲解了AI领域的核心概念、进阶技术和未来趋势。从基础模型架构、训练与微调流程,到关键组件与工程、推理参数与问题,再到评估指标、安全伦理、硬件生态工具和宏观未来概…

2026/7/8 9:35:19 阅读更多 →

最新新闻

STM32F411RE与TB67H480FNG的电机控制方案解析

STM32F411RE与TB67H480FNG的电机控制方案解析

1. 为什么选择TB67H480FNGSTM32F411RE组合 在电机控制和嵌入式系统开发领域,硬件选型往往决定了项目的性能上限和开发效率。TB67H480FNG作为东芝新一代步进电机驱动芯片,与STMicroelectronics的STM32F411RE微控制器组合,形成了工业级运动控制…

2026/7/8 10:49:37 阅读更多 →
终极Unity资源编辑器:UABEA完整指南与快速入门教程

终极Unity资源编辑器:UABEA完整指南与快速入门教程

终极Unity资源编辑器:UABEA完整指南与快速入门教程 【免费下载链接】UABEA c# uabe for newer versions of unity 项目地址: https://gitcode.com/gh_mirrors/ua/UABEA UABEA是一款功能强大的跨平台Unity Asset Bundle编辑器,专为游戏开发者和资源…

2026/7/8 10:45:31 阅读更多 →
5分钟免费安装WarcraftHelper:魔兽争霸3性能优化与兼容性修复终极指南

5分钟免费安装WarcraftHelper:魔兽争霸3性能优化与兼容性修复终极指南

5分钟免费安装WarcraftHelper:魔兽争霸3性能优化与兼容性修复终极指南 【免费下载链接】WarcraftHelper Warcraft III Helper , support 1.20e, 1.24e, 1.26a, 1.27a, 1.27b 项目地址: https://gitcode.com/gh_mirrors/wa/WarcraftHelper 还在为魔兽争霸3在现…

2026/7/8 10:45:31 阅读更多 →
中国到中东物流推荐及选择指南

中国到中东物流推荐及选择指南

中国到中东物流现状分析在当前跨境电商快速发展的环境中,从中国到中东的物流服务日益重要。物流公司如亿俐缇(YLT GLOBAL)在行业内已建立起高效的服务体系,以适应市场的变化。许多企业正积极开拓代发、清关与中转等多样化服务,以满足不同客户…

2026/7/8 10:41:30 阅读更多 →
Tensor 内存布局解析:从 NumPy 到 PyTorch 2.0 的 contiguous 与 view 避坑指南

Tensor 内存布局解析:从 NumPy 到 PyTorch 2.0 的 contiguous 与 view 避坑指南

Tensor 内存布局解析:从 NumPy 到 PyTorch 2.0 的 contiguous 与 view 避坑指南 在深度学习与科学计算领域,Tensor 作为核心数据结构,其内存布局直接影响计算效率与代码正确性。本文将深入解析 PyTorch 中 contiguous() 与 view() 的底层…

2026/7/8 10:41:30 阅读更多 →
ICLR 2026 | 用少量标注构造多步推理数据:COGS 用合成数据和过程奖励增强多模态大模型

ICLR 2026 | 用少量标注构造多步推理数据:COGS 用合成数据和过程奖励增强多模态大模型

在图表问答、网页理解这类任务里,图像资源通常并不稀缺,稀缺的是高质量的多步推理数据。模型要先识别图中的数值或对象,再完成比较、计数、计算甚至趋势推断,其中任意一步出错,最终答案就会偏离。人工标注这样的训练数…

2026/7/8 10:39:29 阅读更多 →

日新闻

3大核心能力重塑《明日方舟》游戏体验:MAA自动化助手的革命性突破

3大核心能力重塑《明日方舟》游戏体验:MAA自动化助手的革命性突破

3大核心能力重塑《明日方舟》游戏体验:MAA自动化助手的革命性突破 【免费下载链接】MaaAssistantArknights 《明日方舟》小助手,全日常一键长草!| A one-click tool for the daily tasks of Arknights, supporting all clients. 项目地址: …

2026/7/8 0:00:48 阅读更多 →
MyBatis 批量操作深度优化——从 N+1 到批处理的全路径

MyBatis 批量操作深度优化——从 N+1 到批处理的全路径

MyBatis 批量操作深度优化——从 N1 到批处理的全路径 一、从"功能正确"到"性能可接受"——MyBatis 批量操作的三段式进化 MyBatis 在日常增删改查场景中几乎是无感的——实体映射直观、SQL 控制灵活。但当数据量从千级上升到十万级、百万级,许…

2026/7/8 0:00:48 阅读更多 →
工业负载控制方案:TPD2015FN与PIC18F45K22应用解析

工业负载控制方案:TPD2015FN与PIC18F45K22应用解析

1. 工业负载控制方案概述在工业自动化、电机驱动和照明控制等高需求场景中,可靠地控制电感和电阻负载是核心挑战之一。TPD2015FN作为东芝的8通道高端智能功率开关IC,配合PIC18F45K22微控制器,能够构建一套稳定、高效的负载控制系统。这套组合…

2026/7/8 0:02:48 阅读更多 →

周新闻

B站视频下载神器BiliTools:5分钟学会轻松保存任何B站内容

B站视频下载神器BiliTools:5分钟学会轻松保存任何B站内容

B站视频下载神器BiliTools:5分钟学会轻松保存任何B站内容 【免费下载链接】BiliTools A cross-platform bilibili toolbox. 跨平台哔哩哔哩工具箱,支持下载视频、番剧等等各类资源 项目地址: https://gitcode.com/GitHub_Trending/bilit/BiliTools …

2026/7/7 14:24:45 阅读更多 →
威胁模型全解析:从新手入门到实战应用,助你构建安全产品!

威胁模型全解析:从新手入门到实战应用,助你构建安全产品!

威胁模型的陌生现状在忙碌疲惫的一天里,参与了关于混合后量子密码学的讨论,应付端点攻击找茬的人,还参与留言板讨论后,发现“威胁模型”对多数人仍是陌生概念,且多被当作时髦用语。有趣的相关画作有一幅由 Embyr 创作的…

2026/7/7 12:34:47 阅读更多 →
渗透测试入门指南:从零基础到实战环境搭建

渗透测试入门指南:从零基础到实战环境搭建

1. 从“看热闹”到“入门”:我理解的渗透测试到底是什么?每次看到新闻里说某个大公司的数据被“黑”了,或者某个网站被攻击导致服务瘫痪,你是不是和我一样,心里会冒出两个念头:一是“这黑客真厉害”&#x…

2026/7/7 15:59:06 阅读更多 →

月新闻