1. First, the system will issue a standard broadcast action, called Android. Intent. Action. boot_completed. This action will only be sent once after the instance is started.
2. Construct an intentreceiver class and reconstruct its abstract method onreceiveintent (context, intent) to start the service you want to start.
3. In androidmanifest. XML, first add it to get the boot_completed license, then register the previously reconstructed intentreceiver class, and add it to it so that it can capture this action.
Java code
- <Uses-Permission Android: Name = "android. Permission. receive_boot_completed"/>
- <Cycler Android: Name = ". Olympus sreceiver" Android: Label = "@ string/app_name">
- <Intent-filter>
- <Action android: name = "android. intent. action. BOOT_COMPLETED"/>
- <Category android: name = "android. intent. category. LAUNCHER"/>
- </Intent-filter>
- </Cycler>
Java code
- Public class OlympicsReceiver extends IntentReceiver
- {
- /* 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,
- Olympus service. class), null); // starts the countdown service.
- Toast. maketext (context, "Olympus sreminder service has started! ", Toast. length_long)
- . Show ();
- }
- }
- }
Note: The current intentreceiver has changed to broadcastreceiver, and onreceiveintent is onreceive. Therefore, the Java code is as follows:
(The application can also be automatically started upon startup)
Java code
- Public class olympicsreceiver extends broadcastreceiver
- {
- /* 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,
- Olympus service. Class), null); // starts the countdown service.
- Toast. maketext (context, "Olympus sreminder service has started! ", Toast. length_long)
- . Show ();
- // You can add the application code that is automatically started upon startup.
- }
- }
- }