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

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之前就有,无法确认。

相关文章:

  • docker网络相关内容详解
  • 品优购项目(HTML\CSS)
  • 2.linux目录切换命令:cd与pwd以及路径与路径符
  • 家庭智能监控系统实现实时监控主要依托传感器网络
  • C++ RB_Tree
  • nginx源码下载和测试
  • spring4第4课-ioc控制反转-详解如何注入参数
  • Linux环境基础开发工具->gcc/g++
  • 深入理解C#中的委托与事件:从基础到高级应用
  • 图片压缩工具 | 发布到咸鱼并配置网盘自动发货
  • 如何利用categraf的exec插件实现对Linux主机系统用户及密码有效期进行监控及告警?
  • 【Redis技术进阶之路】「原理分析系列开篇」探索事件驱动枚型与数据特久化原理实现(文件事件驱动执行控制)
  • C# Costura.Fody 排除多个指定dll
  • Cobra CLI 工具使用指南:构建 Go 语言命令行应用的完整教程
  • Java面试实战:从Spring到大数据的全栈挑战
  • QT6搭建和使用MQTT
  • 【LangChain】
  • 【Redis】第3节|深入理解Redis线程模型
  • Python中的__init__和__new__方法解析
  • 纵览网丨病毒学领域的 AI 变局:机遇、隐忧与监管之路
  • 做网站大图片/怎么创造自己的网站
  • 一家专做中式设计的网站/chatgpt入口
  • 兰州网站推/新闻平台发布
  • 做电影网站采集什么意思/百度推广服务费一年多少钱
  • 阿里云网站模板送域名/网站关键词排名怎么优化
  • 网站设计就业怎么样/seo网站推广是什么