How Android lets a service boot up automatically

Source: Internet
Author: User

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
    1. <uses-permission android:name="Android.permission.RECEIVE_BOOT_COMPLETED" ></uses-permission>
    2. <receiver android:name=". Olympicsreceiver "android:label=" @string/app_name ">
    3. <intent-filter>
    4. <action android:name="Android.intent.action.BOOT_COMPLETED"/>
    5. <category android:name="Android.intent.category.LAUNCHER"/>
    6. </intent-filter>
    7. </receiver>
[Java]View Plaincopy
    1. <uses-permission android:name= " Android.permission.RECEIVE_BOOT_COMPLETED "></USES-PERMISSION>&NBSP;&NBSP;
    2. <receiver  android:name=" @string/app_name ">&NBSP;&NBSP;&NBSP;
    3. &NBSP;&NBSP;&NBSP;&NBSP;<INTENT-FILTER>&NBSP;&NBSP;&NBSP;
    4.         <action android:name= " Android.intent.action.BOOT_COMPLETED "&NBSP;/>&NBSP;&NBSP;&NBSP;
    5.         <category android:name= " Android.intent.category.LAUNCHER "&NBSP;/>&NBSP;&NBSP;&NBSP;
    6.     </ intent-filter>   
    7. </RECEIVER>&NBSP;&NBSP;

Java:

Java code
  1. Public class olympicsreceiver extends intentreceiver
  2. {
  3. / * The intent source to receive * /
  4. static final String ACTION = "Android.intent.action.BOOT_COMPLETED";
  5. Public void onreceiveintent (context context, Intent Intent)
  6. {
  7. If (Intent.getaction (). Equals (ACTION))
  8. {
  9. Context.startservice (new Intent (context,
  10. Olympicsservice. class), null); //Start Countdown service
  11. Toast.maketext (Context, "Olympicsreminder service has started!", Toast.length_long). Show ();
  12. }
  13. }
  14. }
[Java]View Plaincopy
  1. Public class Olympicsreceiver extends Intentreceiver
  2. {
  3. / * The intent source to receive * /
  4. static final String ACTION = "Android.intent.action.BOOT_COMPLETED";
  5. public void Onreceiveintent (context context, Intent Intent)
  6. {
  7. if (intent.getaction (). Equals (ACTION))
  8. {
  9. Context.startservice (new Intent (context,
  10. Olympicsservice. class), null); //Start Countdown service
  11. Toast.maketext (Context, "Olympicsreminder service has started!", Toast.length_long). Show ();
  12. }
  13. }
  14. }

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
  1. Public class olympicsreceiver extends broadcastreceiver
  2. {
  3. / * The intent source to receive * /
  4. static final String ACTION = "Android.intent.action.BOOT_COMPLETED";
  5. Public void onreceive (context context, Intent Intent)
  6. {
  7. If (Intent.getaction (). Equals (ACTION))
  8. {
  9. Context.startservice (new Intent (context,
  10. Olympicsservice. class), null); //Start Countdown service
  11. Toast.maketext (Context, "Olympicsreminder service has started!", Toast.length_long). Show ();
  12. //This side can add the application code to start the boot automatically
  13. }
  14. }
  15. }

How Android lets a service boot up automatically

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.