Solution to bindservice failure in tabactivity

Source: Internet
Author: User

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 );

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.