Solution to bindservice failure in tabactivity
Problem description:
In a single activity, startservice bindservice is normal. After the activity is put into the tabactivity framework, the serviceconnection of bindservice cannot return the BIND object.
The results of Multiple tests showed that Google Android issue had this defect. Here is the detailed information about the defect (Google Android issue 2483)
Solution:
Using getapplicationcontext (). bindservice instead of just bindservice on your activity solves the problem as it is using the higher level application context.
You need to call getapplicationcontext () to obtain the Context Environment of the activity to be normal bindservice,
Use this. getapplicationcontext (). bindservice ([ARGs…]) Replace the original bindservice ([ARGs…]), Otherwise, the system will always fail and return false, and the remote service will return null.
Instance code:
Mserviceconnection = new serviceconnection () {@ overridepublic void onserviceconnected (componentname, ibinder binder) {log. I (TAG, "onserviceconnected") ;}@ overridepublic void onservicedisconnected (componentname) {log. I (TAG, "onservicedisconnected") ;}}; // This is required in tabhost. getapplicationcontext (). bindservicethis. getapplicationcontext (). bindservice (new intent (this, hrmservice. class), mserviceconnection, service. bind_auto_create );