Android boot broadcast and shutdown Broadcast

Source: Internet
Author: User

Sometimes we need our program to run automatically after the system starts up. At this time, we can use the Broadcast Mechanism in Android to compile a class that inherits BroadcastReceiver and accept the system to start and stop broadcasting. The Code is as follows:

[Java]View plaincopy
  1. /**
  2. * @ Author coolszy
  3. * @ Date 2011-6-14
  4. * @ Blog http://blog.csdn.net/coolszy
  5. */
  6. Public class BootCompletedReceiver extends BroadcastReceiver
  7. {
  8. @ Override
  9. Public void onReceive (Context context, Intent intent)
  10. {
  11. Log. I ("MainActivity", "system started ");
  12. }
  13. }
Then, register in the AndroidManifest. xml file:
[Xhtml]View plaincopy
  1. <Cycler android: name = ". BootCompletedReceiver">
  2. <Intent-filter>
  3. <Action android: name = "android. intent. action. BOOT_COMPLETED"/>
  4. </Intent-filter>
  5. </Cycler>

Add the required permissions:

 

[Xhtml]View plaincopy
  1. <Uses-permission android: name = "android. permission. RECEIVE_BOOT_COMPLETED"/>
 

Since the Android system sends a broadcast after it is started, is there a broadcast when the system is disabled? Query the help document and find the system-disabled broadcast:

 

[Java]View plaincopy
  1. /**
  2. * @ Author coolszy
  3. * @ Date 2011-6-14
  4. * @ Blog http://blog.csdn.net/coolszy
  5. */
  6. Public class ShutdownReceiver extends BroadcastReceiver
  7. {
  8. @ Override
  9. Public void onReceive (Context context, Intent intent)
  10. {
  11. Log. I ("MainActivity", "starting to close ...");
  12. }
  13. }

Register in the AndroidManifest. xml file:

[Xhtml]View plaincopy
  1. <Cycler android: name = ". shutdowncycler">
  2. <Intent-filter>
  3. <Action android: name = "android. intent. action. ACTION_SHUTDOWN"/>
  4. </Intent-filter>
  5. </Cycler>
Do you still need the corresponding permissions? After querying the help document, you cannot find the relevant permissions. Test the help document in the simulator. After the system is disabled, the information can be output normally.
Related Article

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.