I. First, make a listener:
Public class startbroadcastreceiver extends broadcastreceiver {<br/> Private Static final string action = "android. intent. action. boot_completed "; <br/> Public void onreceive (context, intent) {<br/> If (intent. getaction (). equals (Action) {<br/> intent I = new intent (intent. action_run); <br/> I. setclass (context, tservice. class); <br/> context. startservice (I); <br/>}</P> <p>}
2. Create another service:
Package COM. testservice; <br/> Import android. app. notification; <br/> Import android. app. icationicationmanager; <br/> Import android. app. pendingintent; <br/> Import android. app. service; <br/> Import android. content. intent; <br/> Import android. OS. binder; <br/> Import android. OS. handler; <br/> Import android. OS. ibinder; <br/> Import android. util. log; <br/> public class tservice extends Service {<br/>/** <br/> * Create a handler object, use postdelayed as a process <br/> */<br/> private handler objhandler = new handler (); <br/> private int intcounter = 0; <br/> Private Static final string tag = "tservice"; <br/> private icationicationmanager notificationmanager; <br/> private runnable mtasks = new runnable () {<br/> Public void run () {<br/> intcounter ++; <br/> log. I ("Hippo", "counter:" + integer. tostring (intcounter); <br/> objhandler. postdelayed (mtasks, 1000); <br/>}< br/>}; <br/> Public void oncreate () {<br/> log. D (TAG, "==============> tservice. oncreate "); <br/> icationicationmanager = (notificationmanager) getsystemservice (icationication_service); <br/> shownotification (); <br/> super. oncreate (); <br/>}</P> <p> Public void onstart (intent, int startid) {<br/> log. I (TAG, "==============> tservice. onstart "); <br/> objhandler. postdelayed (mtasks, 1000); <br/> super. onstart (intent, startid); <br/>}< br/> Public ibinder onbind (intent) {<br/> log. I (TAG, "==============> tservice. onbind "); <br/> return NULL; <br/>}< br/> public class localbinder extends binder {<br/> Public tservice getservice () {<br/> return tservice. this; <br/>}< br/> Public Boolean onunbind (intent) {<br/> log. I (TAG, "==============> tservice. onunbind "); <br/> return false; <br/>}< br/> Public void onrebind (intent) {<br/> log. I (TAG, "==============> tservice. onrebind "); <br/>}< br/> Public void ondestroy () {<br/> log. I (TAG, "==============> tservice. ondestroy "); <br/> icationicationmanager. cancel (R. string. service_start); <br/> objhandler. removecallbacks (mtasks); <br/> super. ondestroy (); <br/>}< br/> private void shownotification () {<br/> Notification = new notification (R. drawable. icon, <br/> "service start", system. currenttimemillis (); <br/> intent = new intent (this, testservice. class); <br/> intent. putextra ("flg", 1); <br/> pendingintent contentintent = pendingintent. getactivity (this, 0, <br/> intent, 0); <br/> notification. setlatesteventinfo (this, "service", "service start", <br/> contentintent); <br/> icationicationmanager. notify (R. string. service_start, notification); <br/>}< br/>}
3. Create another main program:
Package COM. testservice; <br/> Import android. app. activity; <br/> Import android. content. context; <br/> Import android. content. intent; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. view. view. onclicklistener; <br/> Import android. widget. button; <br/> Import android. widget. toast; <br/> public class testservice extends activity {</P> <p> private button button01; <br/> private button button02; <br/> private int flg; <br/> private intent tsintent; <br/>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); </P> <p> setcontentview (R. layout. main); <br/> button01 = (button) findviewbyid (R. id. button01); <br/> button02 = (button) findviewbyid (R. id. button02); </P> <p> tsintent = This. getintent (); <br/> bundle = tsintent. getextras (); </P> <p> If (bundle = NULL) {<br/> flg = 1; <br/> displaytoast (this, "service start ", 2); <br/> startservice (); <br/> finish (); <br/>}else {<br/> displaytoast (this, "service stop ", 2); <br/> stopservice (); <br/> finish (); <br/>}</P> <p> button01.setonclicklistener (listener01 ); <br/> button02.setonclicklistener (listener02); </P> <p >}</P> <p> button. onclicklistener listener01 = new onclicklistener () {<br/> @ override <br/> Public void onclick (view V) {<br/> // todo auto-generated method stub <br/> startservice (); <br/>}</P> <p> }; </P> <p> button. onclicklistener listener02 = new onclicklistener () {<br/> @ override <br/> Public void onclick (view V) {<br/> // todo auto-generated method stub <br/> stopservice (); <br/>}</P> <p> }; </P> <p> private void startservice () {<br/> intent I = new intent (testservice. this, tservice. class); <br/> I. setflags (intent. flag_activity_new_task); <br/> This. startservice (I); <br/>}</P> <p> private void stopservice () {<br/> intent I = new intent (testservice. this, tservice. class); <br/> This. stopservice (I); <br/>}< br/> Public static void displaytoast (context, string STR, int time) {<br/> If (time = 1) {<br/> // display toast in a short time <br/> toast. maketext (context, STR, toast. length_short ). show (); <br/>}else if (time = 2) {<br/> // display toast for a long time <br/> toast. maketext (context, STR, toast. length_long ). show (); <br/>}< br/>}
4. register the listener and service and activate the permissions in androidmenfest. xml:
1. Receiver:
<Cycler Android: Name = ". startbroadcastreceiver "> <br/> <intent-filter> <br/> <action Android: Name =" android. intent. action. boot_completed "/> <br/> </intent-filter> <br/> </receiver>
2. Service:
<Service android: Name = ". tservice "<br/> Android: Label =" tservice "<br/> Android: icon =" @ drawable/icon "<br/> Android: enabled = "true" <br/> Android: exported = "true" <br/> Android: Process = ": Remote"> <br/> </service>
3. Registration permission:
<Uses-Permission Android: Name = "android. Permission. receive_boot_completed"/>
Note: Both Accept Broadcast and service must be registered at: <Application> Register here </Application>