Bind service calls a method in a local service

Source: Internet
Author: User

If you want to invoke the method in the service, through StartService () can not be done, then need to use Bindservice to solve.

The following demo is a custom method that invokes service in the activity---methodinservice

This demo can resolve the data in the service that was called in the project development.

The proxy mode is used here in the service. This is to add an intermediary between the service component and the activity component.

Passing data through a proxy. This is the Binder object. This agent is the interface IService

The code in the service is as follows:

1  Public classDemoserviceextendsService {2 3     /**4 * Internal Xiaomi, can invoke the internal method of the service5 * Private Xiaomi, build an interface to provide external access to the method6      */7     Private classXiaomiextends Binder implements iservice{8         //methods in the service are called through this method9         //overriding an abstract method in an interfaceTen          Public voidCallmethodinservice (intMoney ) { One             if(Money > 100){ A                 //this invokes the method in the service . - Methodinservice (); -}Else{ theSystem.out.println ("This money still wants to work?")); -             } -         } -          +          Public voidWash the sauna () { -SYSTEM.OUT.PRINTLN ("Wash the sauna Together"); +         } A     } at  -     /** - * This method is called when executing bindservice - * IBinder equivalent to internal Xiaomi -      */ - @Override in      Publicibinder onbind (Intent Intent) { -System.out.println ("2, if the service is successfully bound, the Onbind method will be executed, returning IBinder Xiaomi"); to         return NewXiaomi (); +     } -  the @Override *      Public voidonCreate () { $SYSTEM.OUT.PRINTLN ("Service has been turned on");Panax Notoginseng  -     } the  + @Override A      Public voidOnDestroy () { theSYSTEM.OUT.PRINTLN ("Service has been turned off"); +         Super. OnDestroy (); -     } $      $     /** - * Methods in the service -      */ the      Public voidMethodinservice () { -System.out.println ("6, you have successfully invoked a method in the service");Wuyi     } the  -}

The IService interface code is as follows:

1  Public Interface IService {23      Public void callmethodinservice (int money ); 4     5 }

The code in the activity is as follows:

1  Public classMainactivityextendsActivity {2 3 @Override4     protected voidonCreate (Bundle savedinstancestate) {5         Super. OnCreate (savedinstancestate);6 Setcontentview (r.layout.activity_main);7     }8     9 MyConn Conn;Ten     //object that creates an interface One IService Mybinder; A  -     //Binding Service -      Public voidbindservice (view view) { theSystem.out.println ("1, binding service"); -Intent Intent =NewIntent ( This, Demoservice.class); -conn =Newmyconn (); - Bindservice (Intent, Conn, bind_auto_create); +     } -  +     //Unbind Service A      Public voidunbindservice (view view) { at  - Unbindservice (conn); -     } -  -     /** - * Methods in the call service in      */ -      Public voidcallservice (view view) { to         //through the internal Xiaomi can indirectly invoke the method in the service +System.out.println ("5, using Mybinder to indirectly invoke the method in the service"); -Mybinder.callmethodinservice (500); the     } *      $     Panax Notoginseng     /** - * Service successfully connected to the communication channel the      */ +     Private class MyConn implements serviceconnection{ A  the         //when the service is successfully connected + @Override -          Public voidonserviceconnected (componentname name, IBinder service) { $System.out.println ("3, get a link to the service, get to the Xiaomi"); $             Mybinder = (iservice) service; -System.out.println ("4, cast IBinder to Xiaomi Mybinder"); -         } the  -         //when the service connection is unsuccessfulWuyi @Override the          Public voidonservicedisconnected (componentname name) { -              Wu         } -     } About}

The key code is already marked in red.

In this demo, the activity wants to directly invoke the method in the service is not possible, so you can define a proxy IService interface,

Let the internal class Xiaomi in the service implement the interface and override the abstract method so that it can be called in the activity.

Bind service calls a method in a local service

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.