There are two ways to start a service, one is through the start mode, one is through the bind mode, starting the services through the start mode, the components and services to start the service is not much of a connection. So in order to communicate with the service, only through the bind way. Here are a few steps: (the service in the demo provides a download function, then decides when to start the download in the activity, and check the download progress at any time)
The first step:
Creating an inner class in the service class inherits from the binder (the Onbind method returns the IBinder interface, the Binder class is the implementation class of the IBinder interface) (see here also implements an interface, implements the method in the interface, also provides an additional method, This extra method does not want the activity to be accessible, so here we are through the interface Way):
1.private class Downloadbinder extends Binder implements downloadbinderimpl{@Overridepublic void Startdownload () { LOG.I (TAG, "startdownload");} @Overridepublic int getprogress () {log.i (TAG, "getprogress"); return 0;} public void Inner () {log.i (TAG, "I do not want activity to use Me");}}
Step two: The interface mentioned above:
Package Com.dystu.servicedemo;public interface Downloadbinderimpl {public void Startdownload ();p ublic int getprogress ( );}
Step three: Return the Downloadbinder instance inside the service's Onbind () method:
@Overridepublic ibinder onbind (Intent Intent) {log.i (TAG, "Onbind"); return new Downloadbinder ();}
Fourth step: Bind the service in activity:
public void bind (view view) {Intent Intent = new Intent (this, myservice.class); Bindservice (Intent, Conn, bind_auto_create );}
Fifth Step: Create Serviceconnection Anonymous inner class, implement the Onserviceconnected and Onservicedisconnected methods, these two methods will be called when the service is successfully bound and unbound.
Private class MyConn implements Serviceconnection {@Overridepublic void onserviceconnected (componentname name, IBinder Service) {DBI = (downloadbinderimpl) service; LOG.I (TAG, "successfully bound service----onserviceconnected executed");} @Overridepublic void onservicedisconnected (componentname name) {log.i (TAG, "unbind service----onservicedisconnected executed");}}
Sixth step:
Methods to invoke the service:
public void call (view view) {if (DBI = null) {dbi.startdownload ();d bi.getprogress ();} else {Toast.maketext (this, "bind the service first ", 0). Show ();}}
can also be performed in onserviceconnected.
The effect of the implementation:
Click Bind:
Click Call:
Click Unbind:
The complete code:
Interface:
Package Com.dystu.servicedemo;public interface Downloadbinderimpl {public void Startdownload ();p ublic int getprogress ( );}
Service:
Package Com.dystu.servicedemo;import Android.app.service;import Android.content.intent;import android.os.Binder; Import Android.os.ibinder;import Android.util.log;public class MyService extends Service {private static final String TAG = "MyService";//2. @Overridepublic ibinder onbind (Intent Intent) {log.i (TAG, "Onbind"); return new Downloadbinder ();} @Overridepublic boolean onunbind (Intent Intent) {log.i (TAG, "Onunbind"); return Super.onunbind (Intent);} @Overridepublic void OnCreate () {super.oncreate (); LOG.I (TAG, "onCreate");} @Overridepublic int Onstartcommand (Intent Intent, int flags, int startid) {log.i (TAG, "Onstartcommand"); return Super.onstartcommand (Intent, flags, Startid);} @Overridepublic void OnDestroy () {Super.ondestroy (); LOG.I (TAG, "OnDestroy");} 1.private class Downloadbinder extends Binder implements downloadbinderimpl{@Overridepublic void Startdownload () { LOG.I (TAG, "startdownload");} @Overridepublic int getprogress () {log.i (TAG, "getprogress"); return 0;} public void Inner () {Log. I (TAG, "I don't want activity to use Me");}}}
Main interface layout:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " tools:context= "${relativepackage}.${activityclass}" > <button android:id= "@+id/btn1" Android:onclick= "bind" android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:text= "bind"/> <button android:id= "@+id/btn2" android:layout_below= "@id/btn1" android:onclick= "unbind" android:layout_width= "match_parent" android:layout_height= "Wrap_ Content " android:text=" unbind "/> <button android:layout_below=" @id/btn2 " android:o nclick= "Call" android:layout_width= "match_parent" android:layout_height= "Wrap_content" android: text= "Call"/></relativelayout>
Mainactivity:
Package Com.dystu.servicedemo;import Android.app.activity;import Android.content.componentname;import Android.content.intent;import Android.content.serviceconnection;import Android.os.bundle;import Android.os.ibinder;import Android.util.log;import Android.view.view;import Android.widget.toast;public class Mainactivity extends Activity {public static final String TAG = "mainactivity";p rivate myconn Conn;downloadbinderimpl dbi; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); conn = new myconn ();} 3.public void bind (view view) {Intent Intent = new Intent (this, myservice.class); Bindservice (Intent, Conn, BIND_AUTO_CR eate);} public void Unbind (view view) {Unbindservice (conn);d bi = null;} public void call (view view) {if (DBI = null) {dbi.startdownload ();d bi.getprogress ();} else {Toast.maketext (this, "bind the service first ", 0). Show ();}} 4.private class MyConn implements serviceconnection {@Overridepublic void onserviceconnected (componentname name, IBinder service) {DBI = (downloadbinderimpl) service; LOG.I (TAG, "successfully bound service----onserviceconnected executed");} @Overridepublic void onservicedisconnected (componentname name) {log.i (TAG, "unbind service----onservicedisconnected executed");}}
Ps:
The onservicedisconnected () method in class serviceconnection is normally not called, and it is invoked when the service is destroyed by a different source, such as when the memory is out of resources.
Communication between service and activity