The previous article mentioned the creation process of remote service. Now we need to enable it to automatically start upon startup.
1. Add remoteservicebootreceiver. Java based on the previous code to implement an intent aggreger
Package COM. fly; <br/> Import android. content. broadcastreceiver; <br/> Import android. content. context; <br/> Import android. content. intent; <br/> Import android. util. log; <br/> public class remoteservicebootreceiver extends broadcastreceiver {<br/> Private Static final string tag = "u0fly remoteservicebootreceiver"; <br/> static final string action = "android. intent. action. boot_completed "; <br/> @ override <br/> Public void onreceive (context arg0, intent arg1) {<br/> log. D (TAG, "Boot completed"); <br/> // todo auto-generated method stub <br/> If (arg1.getaction (). equals (Action) {<br/> // service <br/> intent myintent = new intent (arg0, remoteservice. class); <br/> myintent. setaction ("com. fly. remoteservice "); <br/> arg0.startservice (myintent); <br/>}< br/>
2. Add the permission in androidmanifast. xml and register a receiver
<? 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> </P> <p> <Cycler Android: Name = ". remoteservicebootreceiver "> <br/> <intent-filter> <br/> <action Android: Name =" android. intent. action. boot_completed "/> <br/> </intent-filter> <br/> </er ER> </P> <p> </Application> </P> <p> <uses-Permission Android: name = "android. permission. receive_boot_completed "> </uses-Permission> <br/> <uses-SDK Android: minsdkversion =" 7 "/> <br/> </manifest>
3. The following is a simple instance to start the activity at startup:
Running effect:
Code:
Bootsayhello. Java
Package COM. fly; <br/> Import android. app. activity; <br/> Import android. OS. bundle; <br/> public class bootsayhello extends activity {<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); <br/>}< br/>}
Bootbroadcastreceiver. Java
Package COM. fly; <br/> Import android. content. broadcastreceiver; <br/> Import android. content. context; <br/> Import android. content. intent; <br/> public class bootbroadcastreceiver extends broadcastreceiver {<br/> static final string action = "android. intent. action. boot_completed "; <br/> @ override <br/> Public void onreceive (context, intent) {<br/> // todo auto-generated method stub <br/> If (intent. getaction (). equals (Action) {<br/> intent sayhellointent = new intent (context, bootsayhello. class); <br/> sayhellointent. addflags (intent. flag_activity_new_task); <br/> context. startactivity (sayhellointent); <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 = ". bootsayhello "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/> <Cycler Android: Name = ". bootbroadcastreceiver "> <br/> <intent-filter> <br/> <action Android: Name =" android. intent. action. boot_completed "/> <br/> </intent-filter> <br/> </javaser> <br/> </Application> <br/> <uses-Permission Android: name = "android. permission. receive_boot_completed "> </uses-Permission> <br/> <uses-SDK Android: minsdkversion =" 7 "/> <br/> </manifest>