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/7/8 11:34:53 阅读更多 →
一台电脑控制N台手机实现投屏群控操作,搭建引流工作室必备技能

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

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

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

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

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

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

最新新闻

ViGEmBus虚拟手柄驱动:Windows游戏设备兼容性的终极解决方案

ViGEmBus虚拟手柄驱动:Windows游戏设备兼容性的终极解决方案

ViGEmBus虚拟手柄驱动:Windows游戏设备兼容性的终极解决方案 【免费下载链接】ViGEmBus Windows kernel-mode driver emulating well-known USB game controllers. 项目地址: https://gitcode.com/gh_mirrors/vi/ViGEmBus ViGEmBus是一款强大的Windows内核模…

2026/7/8 11:34:46 阅读更多 →
中国大模型领跑全球调用热潮

中国大模型领跑全球调用热潮

📰 每日AI科技资讯速递 日期:2026年7月7日 | 近24小时热点精选 🤖 一、AI大模型与大模型应用 腾讯正式发布新一代AI大模型混元Hy3 7月6日,腾讯发布混元Hy3大模型,智能水平比肩更大尺寸旗舰模型,已在多个…

2026/7/8 11:28:36 阅读更多 →
STM32G474RE与TPA3138D2高保真音频系统设计指南

STM32G474RE与TPA3138D2高保真音频系统设计指南

1. 音频系统升级方案概述在嵌入式音频系统设计中,TPA3138D2与STM32G474RE的组合堪称黄金搭档。这个方案特别适合需要高保真音频输出的便携式设备、智能家居音响系统和车载音频设备。TPA3138D2作为德州仪器推出的高效D类音频放大器,能够在3.5V至14.4V的宽…

2026/7/8 11:26:31 阅读更多 →
IIM-20670工业级IMU与PIC24EP512GU810的硬件适配与运动跟踪算法

IIM-20670工业级IMU与PIC24EP512GU810的硬件适配与运动跟踪算法

1. IIM-20670运动跟踪模块的核心特性解析 IIM-20670是TDK InvenSense推出的一款工业级6轴惯性测量单元(IMU),集成了3轴加速度计和3轴陀螺仪。这款器件最突出的特点是其卓越的环境适应性——加速度计温度稳定性达到1.7g/C,零偏重复性为1mg,在-…

2026/7/8 11:26:31 阅读更多 →
TPIS1S1385红外传感器与PIC18LF46K42微控制器的智能人体检测方案

TPIS1S1385红外传感器与PIC18LF46K42微控制器的智能人体检测方案

1. 项目背景与核心需求 在智能家居和安防领域,人体存在感应和运动检测技术正经历着从简单触发到精准感知的进化。传统PIR(被动红外)传感器只能检测移动的热源,当人体静止时就会失去响应——这导致在智能照明、安防监控等场景中频繁…

2026/7/8 11:26:31 阅读更多 →
人生宏观无解问题的最小单位是什么?

人生宏观无解问题的最小单位是什么?

第一层:什么叫宏观无解问题? 很多人每天都在思考: 我以后会不会成功?人生到底有没有意义?我是不是选错路了?十年以后会怎样?AI会不会取代我?我到底适合什么?这些问题都有…

2026/7/8 11:24:30 阅读更多 →

日新闻

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 阅读更多 →

月新闻