Android------to bind and communicate with the service

Source: Internet
Author: User



To bind the service and communicate with it:

The service should be started and closed using the Bindservice and Unbindservice methods.

Bindservice (Intent service, serviceconnection conn, int flags);

Explanation Parameters:
Service: This parameter specifies the service to be started by intent

Conn: This parameter is an Serviceconnection object that listens to the link between the visitor and the service.
The onserviceconnected (componentname Name,ibinder Service) method of the Serviceconnection object will be recalled when the connection between the visitor and the service succeeds.
When the host process in which the service resides is terminated due to an abort or other reason, the service and the visitor
The onservicedisconnected (componentname name) method of the Serviceconnection object is recalled when the connection is disconnected.

Flags: Specifies that the service is created automatically when binding. This parameter can be specified as 0 (not created automatically) or bind_auto_create (automatically created).

Serviceconnection:interface for monitoring the state of an application service
Is the interface used to monitor the status of app services.
There are only two methods in this interface:

onserviceconnected (componentname name, IBinder service): Called when a service is connected

onservicedisconnected (componentname name): Called when a connection is disconnected


When the service class is developed, the service class must provide a IBinder Onbind (Intent Intent) method, in the case of binding the ground service
The IBinder object returned by the Onbind method is passed to the service parameter of the Onserviceconnected method in the Serviceconnection object.
This allows the visitor to communicate with the service through the IBinder object.

In the actual development, we usually adopt the way of inheriting binder (IBinder implementation Class) to implement our IBinder object.



Local Service class:


public class Bindservice extends Service {private int count;private boolean quit;//definition Onbinder returned object private Mybinder binder = new Mybinde R ();//By inheriting the binder class to implement the IBinder class public class Mybinder extends Binder {public int getcount () {//Get the running state of the service return count;}} You must implement a method that binds the service when the callback returns the IBinder object @overridepublic ibinder onbind (Intent Intent) {System.out.println ("service is Binded "); return binder;} The method @overridepublic void OnCreate () {super.oncreate () when the service was created; System.out.println ("Service is created"), new Thread () {@Overridepublic void run () {while (!quit) {try {thread.sleep (1000 );} catch (Exception e) {//Todo:handle exception}count++;}}}. Start ();} @Overridepublic boolean onunbind (Intent Intent) {System.out.println ("Service is unbinded"); return true;} @Overridepublic void OnDestroy () {Super.ondestroy (); this.quit = true; System.out.println ("Service is destoryed");}} 


Bind it:


Defines an activity that binds the service.
The inside of the service is accessed through the Mybinder object in activity.


public class Bindservicetest extends Activity {Button bind, unbind, getservicestatus; Bindservice.mybinder binder;private Serviceconnection conn = new Serviceconnection () {@Overridepublic void onservicedisconnected (componentname name) {System.out.println ("--service disconnected--");} @Overridepublic void onserviceconnected (componentname name, IBinder service) {System.out.println ("--service connected--"); binder = (mybinder) service;}}; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.main); bind = (Button) Findviewbyid (r.id.bind); unbind = (Button) Findviewbyid (r.id.unbind); getservicestatus = (button) Findviewbyid (r.id.getservicestatus), final Intent Intent = new Intent (); Intent.setaction (" Com.example.bindservicedemo.BindService "); Bind.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {//bind specified Servicebindservice (intent, Conn, service.bind_auto_create);}); Unbind.setonclicklistener (New OnclicklisTener () {@Overridepublic void OnClick (View v) {//unbound serviceunbindservice (conn);}}); Getservicestatus.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {// Gets and displays the service's count value Toast.maketext (bindservicetest.this, "service count value is:" + binder.getcount (), 1). Show ();}});}}


Printing results:





Android------to bind and communicate with the 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.