Interface:
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent" > <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Open service"Android:onclick= "Start" /> <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Close Service"Android:onclick= "Close" /> <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Bind service"Android:onclick= "Bind" /> <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Unbind Service"Android:onclick= "Unbind" /> <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "method function to invoke service ()"Android:onclick= "CallFunction" /></LinearLayout>
Activity
Packagecom.example.serviceTest;Importandroid.app.Activity;ImportAndroid.content.ComponentName;Importandroid.content.Intent;Importandroid.content.ServiceConnection;ImportAndroid.os.Bundle;ImportAndroid.os.IBinder;ImportAndroid.view.View; Public classMyActivityextendsActivity {PrivateIService Mybinder; PrivateServiceconnection myconn =Newmyconn (); @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.main); } Public voidStart (view view) {StartService (NewIntent ( This, Testservice.class)); } /*** Multiple calls to stop service, no problem (cannot be unbound multiple times)*/ Public voidClose (view view) {StopService (NewIntent ( This, Testservice.class)); } Public voidbind (view view) {Intent Intent=NewIntent ( This, Testservice.class); Bindservice (Intent, myconn, bind_auto_create); } /*** Multiple Unbind service will report an error*/ Public voidUnbind (view view) {Unbindservice (myconn); } Public voidcallfunction (view view) {if(Mybinder! =NULL) {mybinder.callfunction (); } } //when binding, some of the callback methods classMyConnImplementsserviceconnection {@Override Public voidonserviceconnected (componentname name, IBinder service) {System.out.println ("Get to Binder"); Mybinder=(iservice) service; } @Override Public voidonservicedisconnected (componentname name) {System.out.println ("Com.example.serviceTest.MyActivity.MyConn.onServiceDisconnected"); } }}
Service
Packagecom.example.serviceTest;ImportAndroid.app.Service;Importandroid.content.Intent;ImportAndroid.os.Binder;ImportAndroid.os.IBinder;ImportAndroid.widget.Toast;/*** Created by Heyiyong on 14-5-16.*/ Public classTestserviceextendsService { Publicibinder onbind (Intent Intent) {System.out.println ("The service is tied up!" "); return NewMybinder (); } //Intermediary (agent of service) Private classMybinderextendsBinderImplementsiservice{ Public voidcallfunction () {function (); }} @Override Public voidonCreate () {System.out.println ("Com.example.serviceTest.TestService.onCreate"); } @Override Public intOnstartcommand (Intent Intent,intFlagsintStartid) {System.out.println ("Com.example.serviceTest.TestService.onStartCommand"); return Super. Onstartcommand (Intent, flags, Startid); } @Override Public voidOnDestroy () {System.out.println ("Com.example.serviceTest.TestService.onDestroy"); } @Override Public BooleanOnunbind (Intent Intent) {System.out.println ("Com.example.serviceTest.TestService.onUnbind"); return Super. Onunbind (Intent); } Public voidfunction () {Toast.maketext (), Getapplicationcontext (),The function () method is called! ", 1). Show (); System.out.println ("Com.example.serviceTest.TestService.function"); }}