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