a) code for the Mainactivity class
1 PackageCom.example.cms.service;2 3 ImportAndroid.app.Service;4 ImportAndroid.content.ComponentName;5 Importandroid.content.Intent;6 Importandroid.content.ServiceConnection;7 ImportAndroid.os.IBinder;8 Importandroid.support.v7.app.AppCompatActivity;9 ImportAndroid.os.Bundle;Ten ImportAndroid.view.View; One ImportAndroid.widget.Button; A ImportAndroid.widget.Toast; - - Public classMainactivityextendsappcompatactivity { the PrivateButton bind,unbind,getservicestatus; - Bindservice.mybinder Binder; - PrivateServiceconnection conn=Newserviceconnection () { - @Override + Public voidonserviceconnected (componentname name, IBinder service) {//service is returned by the Onbinder () method in the service. -SYSTEM.OUT.PRINTLN ("----Service Connected-----"); +Binder=(bindservice.mybinder) service; A } at @Override - Public voidonservicedisconnected (componentname name) { -SYSTEM.OUT.PRINTLN ("----Service disconnted----"); - } - }; - in @Override - protected voidonCreate (Bundle savedinstancestate) { to Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.activity_main); -Bind=(Button) Findviewbyid (r.id.bind); theUnbind=(Button) Findviewbyid (r.id.unbind); *getservicestatus=(Button) Findviewbyid (r.id.getservicestatus); $ FinalIntent intent=NewIntent ( This, Bindservice.class);Panax NotoginsengBind.setonclicklistener (NewView.onclicklistener () { - @Override the Public voidOnClick (View v) { + //binding specified service A Bindservice (Intent,conn, service.bind_auto_create); the } + }); -Unbind.setonclicklistener (NewView.onclicklistener () { $ @Override $ Public voidOnClick (View v) { - //Contact Binding - Unbindservice (conn); the } - });WuyiGetservicestatus.setonclicklistener (NewView.onclicklistener () { the @Override - Public voidOnClick (View v) { WuToast.maketext (mainactivity. This, the count value for Service is: "+Binder.getcount (), Toast.length_short). Show (); - } About }); $ } - -}
B) Service Class code
PackageCom.example.cms.service;ImportAndroid.app.Service;Importandroid.content.Intent;ImportAndroid.os.Binder;ImportAndroid.os.IBinder;Importandroid.support.annotation.Nullable;/*** Created by CMS on 2016/3/28.*/ Public classBindserviceextendsService {Private intcount; Private Booleanquit=false; PrivateMybinder binder=NewMybinder (); classMybinderextendsbinder{ Public intGetCount () {returncount; }} @Override Publicibinder onbind (Intent Intent) {System.out.print ("Service is binded"); returnBinder; } @Override Public voidonCreate () {Super. OnCreate (); System.out.print ("Service is Created"); NewThread () { Public voidrun () { while(!quit) { Try{Thread.Sleep (1000); } Catch(interruptedexception e) {e.printstacktrace (); } Count++; }}}.start (); } @Override Public BooleanOnunbind (Intent Intent) {System.out.println ("Service is unbinded"); return true; } @Override Public voidOnDestroy () {Super. OnDestroy (); This. quit=true; System.out.println ("Service ID destroyed"); }}
Android Learning Path--service and activity communication