Android boot auto-start Principle and Implementation case

Source: Internet
Author: User

Principle:
The Android system registers registration events (intent) in the system by applications to respond to various types of messages generated by the system. The Android system provides a large number of APIS for application management. You can configure intent and permission to implement various functions.
Auto Start
<Intent-filter>
<Action Android: Name = "android. Intent. Action. boot_completed"/>
<Category Android: Name = "android. Intent. Category. Home"/>
</Intent-filter>
And permission <uses-Permission Android: Name = "android. Permission. receive_boot_completed"> </uses-Permission>.

Example:

1. Create a broadcast receiver class: [Java]

/** $ Filename: bootbroadcastreceiver. java, V $ * $ Date: 2013-6-7 $ * copyright (c) zhenghaibo, Inc. all rights reserved. * This software is made by zhenghaibo. */package njupt. ZHB. startyouself; import android. content. broadcastreceiver; import android. content. context; import android. content. intent;/** @ Author: zhenghaibo * Web: http://blog.csdn.net/nuptboyzhb * mail: zhb931706659@126.com * 2013-6-7 Nanjing, njupt, China */public class bootbroadcastreceiver extends broadcastreceiver {static final string action_boot = "android. intent. action. boot_completed "; @ override public void onreceive (context, intent) {If (intent. getaction (). equals (action_boot) {intent startintent = new intent (context, mainactivity. class); // After receiving the broadcast, jump to mainactivity startintent. addflags (intent. flag_activity_new_task); context. startactivity (startintent );}}}

2. register the broadcast receiver in the manifest. xml file and Configure permissions.
Register a broadcast receiver:

<! -- Register the system broadcast Receiver --> <receiver er Android: Name = ". bootbroadcastreceiver "> <intent-filter> <action Android: Name =" android. intent. action. boot_completed "/> <category Android: Name =" android. intent. category. home "/> </intent-filter> </Cycler>

Add permission:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>

After completing the preceding steps, start a program to complete registration. The software will be automatically started the next time the phone starts up.
Extension: not only the activity can be started automatically, but also a background service can be started. You only need to modify the content in the onreceive function in the receiver!

Source code download for the project: http://download.csdn.net/detail/nuptboyzhb/5541305

Statement: it cannot be used for commercial purposes without permission!

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.