【转】Android TabActivity无法正常bindService解决方法

在Android开发的过程中,我们很有可能会使用到TabActivity来进行开发,而如果你的程序中需要针对TabActivity的TabHost中的每一个Activity绑定一个Service,通常我们的做法是在对应Tab页的Activity的onCreate()方法中进行bind操作,但是通过实践表明这个方法是无法达到绑定效果,在网上查了一下,发现在Google Android Issue中有这个缺陷,缺陷详细信息在这里(Google Android Issue 2483),三楼的oliver给出了正解

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。

作者:newcj
出处:http://newcj.cnblogs.com/
欢迎转载,但还请尊重劳动果实,保留此段声明并注明原文链接。

原文地址:https://www.cnblogs.com/newcj/p/2075791.html