1. First boot the system will issue a standard broadcast action, named Android.intent.action.BOOT_COMPLETED, this Action will only be issued once.
2. Construct a Intentreceiver class, refactor its abstract method onreceiveintent (context context, Intent Intent), where you start the service you want to start.
3. In androidmanifest. XML, first add <uses-permission android:name= "Android.permission.RECEIVE_BOOT_COMPLETED" ></uses-permission > To obtain the boot_completed license, then register the previously refactored Intentreceiver class and add <action android:name= to its <intent-filter> " Android.intent.action.BOOT_COMPLETED "/> So that it can capture this action.
An example
Xml:
Java code
- <uses-permission android:name="Android.permission.RECEIVE_BOOT_COMPLETED" ></uses-permission>
- <receiver android:name=". Olympicsreceiver "android:label=" @string/app_name ">
- <intent-filter>
- <action android:name="Android.intent.action.BOOT_COMPLETED"/>
- <category android:name="Android.intent.category.LAUNCHER"/>
- </intent-filter>
- </receiver>
[Java]View Plaincopy
- <uses-permission android:name= " Android.permission.RECEIVE_BOOT_COMPLETED "></USES-PERMISSION>&NBSP;&NBSP;
- <receiver android:name=" @string/app_name ">&NBSP;&NBSP;&NBSP;
- &NBSP;&NBSP;&NBSP;&NBSP;<INTENT-FILTER>&NBSP;&NBSP;&NBSP;
- <action android:name= " Android.intent.action.BOOT_COMPLETED "&NBSP;/>&NBSP;&NBSP;&NBSP;
- <category android:name= " Android.intent.category.LAUNCHER "&NBSP;/>&NBSP;&NBSP;&NBSP;
- </ intent-filter>
- </RECEIVER>&NBSP;&NBSP;
Java:
Java code
- Public class olympicsreceiver extends intentreceiver
- {
- / * The intent source to receive * /
- static final String ACTION = "Android.intent.action.BOOT_COMPLETED";
- Public void onreceiveintent (context context, Intent Intent)
- {
- If (Intent.getaction (). Equals (ACTION))
- {
- Context.startservice (new Intent (context,
- Olympicsservice. class), null); //Start Countdown service
- Toast.maketext (Context, "Olympicsreminder service has started!", Toast.length_long). Show ();
- }
- }
- }
[Java]View Plaincopy
- Public class Olympicsreceiver extends Intentreceiver
- {
- / * The intent source to receive * /
- static final String ACTION = "Android.intent.action.BOOT_COMPLETED";
- public void Onreceiveintent (context context, Intent Intent)
- {
- if (intent.getaction (). Equals (ACTION))
- {
- Context.startservice (new Intent (context,
- Olympicsservice. class), null); //Start Countdown service
- Toast.maketext (Context, "Olympicsreminder service has started!", Toast.length_long). Show ();
- }
- }
- }
Note: Now the intentreceiver has changed to Broadcastreceiver,onreceiveintent for OnReceive. So the code for this side of Java is:
(can also implement the application boot automatically) Java code
- Public class olympicsreceiver extends broadcastreceiver
- {
- / * The intent source to receive * /
- static final String ACTION = "Android.intent.action.BOOT_COMPLETED";
- Public void onreceive (context context, Intent Intent)
- {
- If (Intent.getaction (). Equals (ACTION))
- {
- Context.startservice (new Intent (context,
- Olympicsservice. class), null); //Start Countdown service
- Toast.maketext (Context, "Olympicsreminder service has started!", Toast.length_long). Show ();
- //This side can add the application code to start the boot automatically
- }
- }
- }
How Android lets a service boot up automatically