Android(安卓)TabActivity中的子Tab Activity 无法正常 bindService 之 解决方法
16lz
2021-01-24
如果使用TabActivity来进行开发,并且程序需要针对TabActivity中TabHost中的每一个Activity单独绑定一个Service,通常做法是在对应Tab页的Activity的onCreate()方法中进行bind service 操作,但是通过实践表明这个方法是无法达到绑定效果,Google Android Issue中有这个缺陷,缺陷详细信息在这里(Google Android Issue 2483)
解决方法:
Using getApplicationContext().bindService instead of just bindService on your activity solves the problem as it is using the higher level application context.
即在TabActivy的TabHost中的Activity如果需要bindService的话,需要先调用getApplicationContext()获取其所属的Activity的上下文环境才能正常bindService,也就是在onCreate()方法中使用this.getApplicationContext().bindService([args…])就可以了,否则bindService将永远失败返回false,remote service 返回也为null。
解决方法:
Using getApplicationContext().bindService instead of just bindService on your activity solves the problem as it is using the higher level application context.
即在TabActivy的TabHost中的Activity如果需要bindService的话,需要先调用getApplicationContext()获取其所属的Activity的上下文环境才能正常bindService,也就是在onCreate()方法中使用this.getApplicationContext().bindService([args…])就可以了,否则bindService将永远失败返回false,remote service 返回也为null。
更多相关文章
- 【Android】Could not find XXX.apk!的解决方法
- Android(安卓)4.4 webview 架构
- 设置背景图时防止图片拉伸的解决方法
- APK的安装过程及原理详解
- Android(安卓)进程和线程(二)
- Android中的线程和线程池
- android中Timer
- Android升级SDK提示:folder failed to be renamed的解决方法
- Android内置多个launcher设置默认launcher