Android bindservice绑定服务,并同步返回service对象的两个方法
先上一段代码:
private IDeviceService deviceService = null;
private ServiceConnection conn=null;
private synchronized void bindyourservice() { Intent intent = new Intent();intent.setPackage("servicepackagename");intent.setAction("serviceactionname");conn=new ServiceConnection() {@Overridepublic void onServiceConnected(ComponentName componentName, IBinder iBinder) { deviceService = IDeviceService.Stub.asInterface(iBinder);}@Overridepublic void onServiceDisconnected(ComponentName componentName) {..................................... }};try {if (bindService(intent, mConn, Service.BIND_AUTO_CREATE)) {...............................} else {....................}} catch (SecurityException e) {.........................}}
这个是最早绑定服务的方式,bindService返回绑定服务状态,访问服务接口需在取到ServiceConnection连接后再访问,整个过程是异步的,而且就算开线程去绑定服务、加锁也没办法在一个方法里面返回这个service对象,原因是这个方法里面的实现:
/frameworks/base/core/java/android/app/ContextImpl.java
看下这个的代码:
public boolean bindService(Intent service, ServiceConnection conn, int flags) {warnIfCallingFromSystemProcess();return bindServiceCommon(service, conn, flags, null, mMainThread.getHandler(), null,getUser());}@Overridepublic boolean bindService(Intent service, int flags, Executor executor, ServiceConnection conn) {return bindServiceCommon(service, conn, flags, null, null, executor, getUser());}@Overridepublic boolean bindIsolatedService(Intent service, int flags, String instanceName,Executor executor, ServiceConnection conn) {warnIfCallingFromSystemProcess();if (instanceName == null) {throw new NullPointerException("null instanceName");}return bindServiceCommon(service, conn, flags, instanceName, null, executor, getUser());}@Overridepublic boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags,UserHandle user) {return bindServiceCommon(service, conn, flags, null, mMainThread.getHandler(), null, user);}/** @hide */@Overridepublic boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags,Handler handler, UserHandle user) {if (handler == null) {throw new IllegalArgumentException("handler must not be null.");}return bindServiceCommon(service, conn, flags, null, handler, null, user);}.....................private boolean bindServiceCommon(Intent service, ServiceConnection conn, int flags,String instanceName, Handler handler, Executor executor, UserHandle user) {....................
}
bindServiceCommon是个私有方法,里面又套了几层代码,太长,翻起来就是整个系统bindservice的流程了。
关键点,bindservice执行的时候接收的Handler是 mMainThread.getHandler(),这个是APP的主线程,也就是无论外部怎么折腾,这个是在主线程接收的。
在上面这段系统代码段里面,有两个方法是可以使用的:
//method 1
public boolean bindService(Intent service, int flags, Executor executor, ServiceConnection conn)
//method 2
/** @hide */
@Override
public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags,Handler handler, UserHandle user)
1、bindService
在bindService有一个传参executor,接收ServiceConnection放到了这个里面:
private boolean[] isBind = {false};
private CountDownLatch latch = new CountDownLatch(1);
private boolean bindyourservice(final Context context){if(deviceService==null){latch = new CountDownLatch(1);Executor executor= Executors.newSingleThreadExecutor();executor.execute(new Runnable() {@Overridepublic void run() {mConn=new ServiceConnection() {@Overridepublic void onServiceConnected(ComponentName className, IBinderiBinder) {deviceService = IDeviceService.Stub.asInterface(iBinder);isBind[0] =true;latch.countDown();}@Overridepublic void onServiceDisconnected(ComponentName className) { isBind[0] =false;latch.countDown();}};Intent intent = new Intent();intent.setPackage("yourservicename");intent.setAction("yourserviceaction");if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {boolean bind=bindService(intent, Context.BIND_AUTO_CREATE,executor,mConn);if(!bind){ isBind[0] =false;latch.countDown();}}}});if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) { try {latch.await();} catch (InterruptedException e) {e.printStackTrace();} if(!isBind[0])return false;}else{ return false;}} return true;}
需要注意的是Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q,这个方法是android7.0及以后提供的。
2、bindServiceAsUser
也可以实现方法1的功能,只不过把接收对象换成了Handler ,bindServiceAsUser是系统隐藏方法,需通过反射方法使用,至于bindServiceAsUser是否早于android7.0之前就有,无法确认。
相关文章:
Android bindservice绑定服务,并同步返回service对象的两个方法
先上一段代码: private IDeviceService deviceService null; private ServiceConnection connnull; private synchronized void bindyourservice() { Intent intent new Intent();intent.setPackage("servicepackagename");intent.setAction("…...
5G 核心网 UE 状态深度剖析:机制、迁移与演进
摘要 本文围绕 5G 核心网中 UE(用户设备)状态展开系统分析,详细阐述了 UE 状态的定义、分类及特点,深入探讨各状态间的迁移流程与关键技术,并结合典型应用场景分析其实际价值。同时,对比 4G 技术剖析 5G 的改进之处,展望 6G 时代 UE 状态管理的演进方向,为 5G 网络优化…...

HomeKit 基本理解
概括 HomeKit 将用户的家庭自动化信息存储在数据库中,该数据库由苹果的内置iOS家庭应用程序、支持HomeKit的应用程序和其他开发人员的应用程序共享。所有这些应用程序都使用HomeKit框架作为对等程序访问数据库. Home 只是相当于 HomeKit 的表现层,其他应用在实现 …...
[SC]SystemC在CPU/GPU验证中的应用(三)
SystemC在CPU/GPU验证中的应用(三) 摘要:下面分享50个逐步升级SystemC编程能力的示例及建议的学习路线图。您可以一次一批地完成它们——从前五个基础的例子开始,然后转向channels, TLM, bus models, simple CPU/GPU kernels等等。在每个阶段掌握之后,再进行下一组…...
gunicorn多线程部署django导致的登陆错误
使用django写后端,认证系统使用了内存中的令牌存储(authentication.py中的user_tokens字典)。 from secrets import token_hex from .models import User# Create a custom token generation function def generate_token():return token_he…...

(LeetCode 每日一题) 909. 蛇梯棋 (广度优先搜索bfs)
题目:909. 蛇梯棋 思路:广度优先搜索bfs队列,时间复杂度0(6*n^2)。 细节看注释 C版本: class Solution { public:int snakesAndLadders(vector<vector<int>>& board) {int nboard.size();// vis[i]:…...
PostgreSQL ERROR: out of shared memory处理
使用pg_dump命令导出一个库的时候,报 pg_dump: error: query failed: ERROR: out of shared memory HINT: You might need to increase "max_locks_per_transaction". 从错误字面上看是超出内存大小了,建议增加max_locks_per_transaction参…...

生成https 证书步骤
一、OpenSSL下载 OpenSSL下载地址: https://slproweb.com/products/Win32OpenSSL.html 如果电脑是64位的就选择64位的 二、OpenSSL安装 双击打开.exe文件 开始安装,一直下一步,不过需要注意的是默认安装路径是C盘,可更改到其他盘…...
34、请求处理-【源码分析】-Model、Map原理
34、请求处理-【源码分析】-Model、Map原理 在 Spring Boot 中,处理请求时,控制器方法可以接收 Model 和 Map 类型的参数,用于向视图传递数据。以下是 Model 和 Map 参数处理的原理分析: ### 1. 参数解析过程 #### **1.1 确定参数…...

设计模式——适配器设计模式(结构型)
摘要 本文详细介绍了适配器设计模式,包括其定义、核心思想、角色、结构、实现方式、适用场景及实战示例。适配器模式是一种结构型设计模式,通过将一个类的接口转换成客户端期望的另一个接口,解决接口不兼容问题,提高系统灵活性和…...

小黑大语言模型通过设计demo进行应用探索:langchain中chain的简单理解demo
chain简介 LangChain 中的 Chain 模块在开发大型语言模型(LLM)驱动的应用程序中起着至关重要的作用。Chain是串联LLM能力与实际业务的关键桥梁,通过将多个工具和模块按逻辑串联起来,实现复杂任务的多步骤流程编排。 案例 通过…...

秒杀系统—5.第二版升级优化的技术文档三
大纲 8.秒杀系统的秒杀库存服务实现 9.秒杀系统的秒杀抢购服务实现 10.秒杀系统的秒杀下单服务实现 11.秒杀系统的页面渲染服务实现 12.秒杀系统的页面发布服务实现 8.秒杀系统的秒杀库存服务实现 (1)秒杀商品的库存在Redis中的结构 (2)库存分片并同步到Redis的实现 (3…...
[SC]SystemC在CPU/GPU验证中的应用(六)
SystemC在CPU/GPU验证中的应用(六) 摘要:下面分享50个逐步升级SystemC编程能力的示例及建议的学习路线图。您可以一次一批地完成它们——从前五个基础的例子开始,然后转向channels, TLM, bus models, simple CPU/GPU kernels等等。在每个阶段掌握之后,再进行下一组…...

【STM32】HAL库 之 CAN 开发指南
基于stm32 f407vet6芯片 使用hal库开发 can 简单讲解一下can的基础使用 CubeMX配置 这里打开CAN1 并且设置好波特率和NVIC相关的配置 波特率使用波特率计算器软件 使用采样率最高的这段 填入 得到波特率1M bit/s 然后编写代码 环形缓冲区 #include "driver_buffer.h&qu…...
WPF的基础设施:XAML基础语法
XAML基础语法 1 控件声明与属性设置1.1 特性语法(Attribute Syntax)1.2 属性元素语法(Property Element Syntax)1.3 特殊值标记扩展 2 x:Name与Name的区别3 注释与代码折叠4 实用技巧集合5 常见错误排查 XAML( Extensible Applic…...

DeepSeek R1-0528 新开源推理模型(免费且快速)
DeepSeek推出了新模型,但这不是R2! R1-0528是DeepSeek的最新模型,在发布仅数小时后就在开源社区获得了巨大关注。 这个悄然发布的模型DeepSeek R1-0528,已经开始与OpenAI的o3一较高下。 让我来详细介绍这次更新的新内容。 DeepSeek R1-0528 发布 DeepSeek在这次发布中采…...

Go 语言的 GC 垃圾回收
序言 垃圾回收(Garbage Collection,简称 GC)机制 是一种自动内存管理技术,主要用于在程序运行时自动识别并释放不再使用的内存空间,防止内存泄漏和不必要的资源浪费。这篇文章让我们来看一下 Go 语言的垃圾回收机制是如…...
[git每日一句]your branch is behind ‘origin/master‘
当 Git 提示 "your branch is behind origin/master" 时,意思是: 你的本地分支落后于远程仓库(origin)的 master 分支 即:远程仓库有新的提交,而你的本地分支尚未同步这些更新。 如何解决&…...
【QT】在QT6中读取文件的方法
在QT6中读取文件的方法 QT6提供了多种读取文件的方式,下面我将介绍几种常用的方法,包括处理文本文件和二进制文件。 1. 使用QFile和QTextStream读取文本文件 这是读取文本文件最常用的方法: #include <QFile> #include <QTextSt…...

安全帽目标检测
安全帽数据集 这里我们使用的安全帽数据集是HelmentDetection,这是一个公开数据集,里面包含5000张voc标注格式的图像,分为三个类别,分别是 0: head 1: helmet 2: person 安全帽数据集下载地址、 我们将数据集下载后,…...
Java工厂方法模式详解
工厂方法模式(Factory Method Pattern)是一种创建型设计模式,它将对象的创建和使用分离,通过定义一个创建对象的接口,让子类决定实例化哪个类。这种模式提高了代码的可扩展性和可维护性,尤其适用于需要根据…...
【pytorch学习】土堆pytorch学习笔记2
说明 主要以https://www.morinha.cc/posts/courses/pytorch-%E5%B0%8F%E5%9C%9F%E5%A0%86的内容为基础,没有的或者自己不是很清楚的再补充上内容,该贴有的内容大部分不再加入进来 新增的更全的参考: https://2048.csdn.net/6801fc28e9858151…...

Eclipse 插件开发 5.3 编辑器 监听输入
Eclipse 插件开发 5.3 编辑器监 听输入 1 插件配置2 添加监听3 查看效果 Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: Click1 Bundle-SymbolicName: com.xu.click1;singleton:true Bundle-Version: 1.0.0 Bundle-Activator: com.xu.click1.Activator Bundle…...

iOS 集成网易云信IM
云信官方文档在这 看官方文档的时候,版本选择最新的V10。 1、CocoPods集成 pod NIMSDK_LITE 2、AppDelegate.m添加头文件 #import <NIMSDK/NIMSDK.h> 3、初始化 NIMSDKOption *mrnn_option [NIMSDKOption optionWithAppKey:"6f6568e354026d2d658a…...
Parasoft C++Test软件单元测试_实例讲解(对多次调用的函数打桩)
系列文章目录 Parasoft C++Test软件静态分析:操作指南(编码规范、质量度量)、常见问题及处理 Parasoft C++Test软件单元测试:操作指南、实例讲解、常见问题及处理 Parasoft C++Test软件集成测试:操作指南、实例讲解、常见问题及处理 进阶扩展:自动生成静态分析文档、自动…...

azure web app创建分步指南系列之二
为注册表授权托管标识 你创建的托管标识尚未获得从容器注册表中提取数据的授权。在此步骤中,你将启用授权。 返回容器注册表的管理页面: 在左侧导航菜单中,选择“访问控制 (IAM)”。选择“添加角色分配”。此屏幕截图显示了如何为容器注册表启用添加角色分配。在角色列表中…...

题海拾贝:P8598 [蓝桥杯 2013 省 AB] 错误票据
Hello大家好!很高兴我们又见面啦!给生活添点passion,开始今天的编程之路! 我的博客:<但凡. 我的专栏:《编程之路》、《数据结构与算法之美》、《题海拾贝》 欢迎点赞,关注! 1、题…...
MySQL 8.0:解析
引言 MySQL 8.0 作为里程碑版本,在功能、性能、安全性等维度进行了全面革新。以下从技术实现、应用场景和实践挑战三个层面,深度解析其核心特性变化: 一、架构级重构:数据字典与原子 DDL 1. 事务性数据字典 技术实现…...

Python量化交易12——Tushare全面获取各种经济金融数据
两年前写过Tushare的简单使用: Python量化交易08——利用Tushare获取日K数据_skshare- 现在更新一下吧,这两年用过不少的金融数据库,akshare,baostock,雅虎的,pd自带的......发现还是Tushare最稳定最好用&…...

封装一个小程序选择器(可多选、单选、搜索)
组件 <template><view class"popup" v-show"show"><view class"bg" tap"cancelMultiple"></view><view class"selectMultiple"><view class"multipleBody"><view class&…...