SRC
── Com
└ ── Fly
── Iremoteservice. aidl
── Remoteserviceactivity. Java
── Remoteservice. Java
Iremoteservice. aidl
Package com. Fly; <br/> interface iremoteservice {<br/> int getcount (); <br/>}
Remoteserviceactivity. Java
Package COM. fly; <br/> Import android. app. activity; <br/> Import android. content. componentname; <br/> Import android. content. context; <br/> Import android. content. intent; <br/> Import android. content. serviceconnection; <br/> Import android. OS. bundle; <br/> Import android. OS. ibinder; <br/> Import android. OS. remoteException; <br/> Import android. util. log; <br/> Import android. view. view; <br/> Import android. WID Get. button; <br/> Import android. widget. textview; <br/> public class remoteserviceactivity extends activity {<br/> Private Static final string tag = "u0fly remoteserviceactivity"; <br/> private button bnstart, bnstop; <br/> private textview text1; <br/> private int count; <br/> private Boolean isbound; </P> <p> private serviceconnection = new serviceconnection () {</P> <p> @ override <br/> pub LIC void onserviceconnected (componentname name, ibinder Service) {<br/> // todo auto-generated method stub <br/> log. D (TAG, "Start on service connected now"); <br/> remoteservice = iremoteservice. stub. asinterface (service); <br/> try {<br/> log. D (TAG, "On Serivce connected, count is" <br/> + remoteservice. getcount (); <br/>} catch (RemoteException e) {<br/> throw new runtimeexception (E); <br/>}< br/>} <Br/> @ override <br/> Public void onservicedisconnected (componentname name) {<br/> // todo auto-generated method stub <br/> log. D (TAG, "Start on service disconnected now"); <br/> remoteservice = NULL; <br/>}< br/>}; <br/> private iremoteservice remoteservice; <br/>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (Savedinstancestate); <br/> setcontentview (R. layout. main); </P> <p> This. bindservice (new intent (remoteserviceactivity. this, remoteservice. class), <br/> This. serviceconnection, context. bind_auto_create); <br/> log. D (TAG, "bind Service now"); <br/> isbound = true; </P> <p> initui (); <br/> log. D (TAG, "init UI now"); <br/> bnstart. setonclicklistener (New button. onclicklistener () {<br/> Public void onclick (view V) {</P> <p> try {<br/> text1.settext ("Start Service now! Count is: "+ remoteservice. getcount (); <br/>} catch (RemoteException e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}< br/> startservice (new intent (remoteserviceactivity. this, remoteservice. class); <br/> log. D (TAG, "Start Service now"); </P> <p >}< br/>}); <br/> bnstop. setonclicklistener (New button. onclicklistener () {<br/> Public void onclick (view v) {<br/> text1.settext ("Stop Service now! "); <Br/> stopservice (new intent (remoteserviceactivity. this, remoteservice. class); <br/> log. D (TAG, "Stop Service now"); <br/>}< br/> }); <br/>}< br/> @ override <br/> protected void ondestroy () {<br/> super. ondestroy (); <br/> log. D (TAG, "==============> ondestroy"); <br/> If (isbound) {<br/> This. unbindservice (serviceconnection); <br/> isbound = false; <br/>}< br/> Public void initui () {<br/> bnstart = (button) findviewbyid (R. id. bnstart); <br/> bnstop = (button) findviewbyid (R. id. bnstop); <br/> text1 = (textview) findviewbyid (R. id. text1); <br/>}< br/>
Remoteservice. Java
Package COM. fly; <br/> Import android. app. service; <br/> Import android. content. intent; <br/> Import android. OS. ibinder; <br/> Import android. OS. remoteException; <br/> Import android. util. log; <br/> public class remoteservice extends Service {<br/> Private Static final string tag = "u0fly remoteservice"; <br/> private int count; <br/> private Boolean threaddisable; <br/> private iremoteservice. stub serviceb Inder = new iremoteservice. stub () {<br/> @ override <br/> Public int getcount () throws RemoteException {<br/> // todo auto-generated method stub <br/> return count; <br/>}< br/> }; <br/> @ override <br/> Public ibinder onbind (intent) {<br/> // todo auto-generated method stub <br/> log. D (TAG, "==============> onbind"); <br/> return servicebinder; <br/>}< br/> @ override <br/> Public Boolean onunbind (inten T I) {<br/> log. E (TAG, "============> onunbind"); <br/> return false; <br/>}< br/> @ override <br/> Public void onrebind (intent I) {<br/> log. E (TAG, "==============> onrebind "); <br/>}< br/> @ override <br/> Public void oncreate () {<br/> super. oncreate (); <br/> log. D (TAG, "==============> oncreate"); <br/> // todo <br/> New thread (New runnable () {</P> <p> @ override <br/> Public void run () {</P> <p> while (! Threaddisable) {<br/> try {<br/> thread. sleep (1000); <br/>} catch (interruptedexception e) {<br/>}< br/> count ++; <br/> log. D (TAG, "Count is" + count); <br/>}< br/> }). start (); <br/>}< br/> @ override <br/> Public void onstart (intent, int startid) {<br/> log. D (TAG, "============> onstart "); <br/>/* <br/> // todo <br/> New thread (New runnable () {<br/> @ override <br/> Public void run () {<Br/> while (! Threaddisable) {<br/> try {<br/> thread. sleep (1000); <br/>} catch (interruptedexception e) {<br/>}< br/> count ++; <br/> log. D (TAG, "Count is" + count); <br/>}< br/> }). start (); */<br/>}< br/> @ override <br/> Public void ondestroy () {<br/> super. ondestroy (); <br/> // todo <br/> This. threaddisable = true; <br/> log. D (TAG, "==============> ondestroy"); <br/>}< br/> Public int getcount () {<br/> return count; <br/>}< br/>}
Androidmanifest. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> package = "com. fly "Android: versioncode =" 1 "Android: versionname =" 1.0 "> <br/> <application Android: icon =" @ drawable/icon "Android: label = "@ string/app_name"> <br/> <activity Android: Name = ". remoteserviceactivity "<br/> Android: Label =" @ string/app_name "> <br/> <intent-filter> <br/> <action Android: Name =" android. intent. action. main "/> <br/> <category Android: Name =" android. intent. category. launcher "/> <br/> </intent-filter> <br/> </activity> <br/> <service android: name = "remoteservice"> <br/> <intent-fliter> <br/> <action Android: Name = "com. fly. remoteservice "/> <br/> </intent-fliter> <br/> </service> <br/> </Application> <br/> <uses-SDK Android: minsdkversion = "7"/> <br/> </manifest>