Android boot auto-start Principle and Implementation case (with source code)

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]Copy codeThe Code is as follows :/*
* $ 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 context, Intent 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:Copy codeThe Code is as follows: <! -- Register a system broadcast Receiver -->
<Cycler 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:Copy codeThe Code is as follows: <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!

Download project source code

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.