Android note-start a Service or Activity

Source: Internet
Author: User

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

  1. <Uses-Permission Android: Name = "android. Permission. receive_boot_completed"/>
  2. <Cycler Android: Name = ". Olympus sreceiver" 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. </Cycler>




Java code

  1. Public class OlympicsReceiver extends IntentReceiver
  2. {
  3. /* 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. Olympus service. class), null); // starts the countdown service.
  11. Toast. maketext (context, "Olympus sreminder service has started! ", Toast. length_long)
  12. . Show ();
  13. }
  14. }
  15. }




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

  1. Public class olympicsreceiver extends broadcastreceiver
  2. {
  3. /* 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. Olympus service. Class), null); // starts the countdown service.
  11. Toast. maketext (context, "Olympus sreminder service has started! ", Toast. length_long)
  12. . Show ();
  13. // You can add the application code that is automatically started upon startup.
  14. }
  15. }
  16. }

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.