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

Android bindservice绑定服务,bindServiceAsUser补充

Android bindservice绑定服务,并同步返回service对象的两个方法-CSDN博客

补充反射并调用bindServiceAsUser的方法:

private boolean initService2(final Context context){if(deviceService==null){latch = new CountDownLatch(1);HandlerThread handlerthread = new HandlerThread("bindservice_thread");handlerthread.start();final Handler handler = new Handler(handlerthread.getLooper());handler.post(new Runnable() {@Overridepublic void run() {try {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("yourservicepackagename");intent.setAction("yourserviceactionname");Method bindServiceAsUser = Context.class.getDeclaredMethod("bindServiceAsUser", Intent.class, ServiceConnection.class,int.class, Handler.class, UserHandle.class);bindServiceAsUser.setAccessible(true);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {boolean bind= (boolean) bindServiceAsUser.invoke(context, intent, mConn, Context.BIND_AUTO_CREATE,handler, UserHandle.getUserHandleForUid(android.os.Process.myUid()));if(!bind){isBind[0] =false;latch.countDown();}}} catch (NoSuchMethodException | IllegalAccessException |InvocationTargetException e) {e.printStackTrace();isBind[0] =false;latch.countDown();}}});try {latch.await();} catch (InterruptedException e) {e.printStackTrace();}if(!isBind[0])return false;}else{return false;}return true;}

http://www.lryc.cn/news/2396774.html

相关文章:

  • [蓝桥杯]交换次数
  • 95套HTML高端大数据可视化大屏源码分享
  • 系统架构设计综合知识与案例分析
  • scale up 不能优化 TCP 聚合性能
  • Python-matplotlib库之核心对象
  • Linux 脚本文件编辑(vim)
  • 学习BI---基本操作---数据集操作
  • 初学大模型部署以及案例应用(windows+wsl+dify+mysql+Ollama+Xinference)
  • AI Agent企业级生产应用全解析
  • RocketMQ 学习
  • 【前端】html2pdf实现用前端下载pdf
  • Redis部署架构详解:原理、场景与最佳实践
  • 前端开发知识体系全景指南
  • C++哈希表:unordered系列容器详解
  • vue-13(延迟加载路由)
  • pom.xml 文件中配置你项目中的外部 jar 包打包方式
  • WordPress通过简码插入bilibili视频
  • ZLG ZCANPro,ECU刷新,bug分享
  • 黑马k8s(十七)
  • 掌握HttpClient技术:从基础到实战(Apache)
  • KEYSIGHT N9320B是德科技N9320B频谱分析仪
  • EXSI通过笔记本wifi上外网配置
  • Java异常处理的全面指南
  • sql知识梳理(超全,超详细,自用)
  • [ Qt ] | QPushButton常见用法
  • WEB3——为什么做NFT铸造平台?
  • 电脑驱动程序更新工具, 3DP Chip 中文绿色版,一键更新驱动!
  • 【机器学习基础】机器学习入门核心:数学基础与Python科学计算库
  • 上交具身机器人的视觉运动导航!HTSCN:融合空间记忆与语义推理认知的导航策略
  • 【C++并发编程01】初识C++并发编程