Android boot auto-Start Program

Source: Internet
Author: User

Background: when Android is started, it sends out a system broadcast with the content action_boot_completed.
The constant of the string is Android. Intent. Action. boot_completed. As longProgramAnd then start
You can. Remember, the android framework says: Don't call me, I'll call you back. What we need to do is to prepare for receiving this message, and
The implementation method is to implement a broadcastreceiver.

1. Interface Activity, bootstartdemo. Java File

 

 
Public class bootstartdemo extends activity {/** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); // No titlerequestwindowfeature (window. feature_no_title); // full screen getwindow (). setflags (windowmanager. layoutparams. flag_fullscreen, windowmanager. layoutparams. flag_fullscreen); setcontentview (R. layout. main); New thread () {public void run () {try {/* 8 seconds later, close the page */sleep (10000);} catch (exception e) {e. printstacktrace () ;}finally {finish (); // close the page }}}. start ();}}

This sectionCodeVery easy. When the activity is started, it will display textview and display the words you want to display, and the page will disappear after only 10 seconds.

2. Receive broadcast messages: bootbroadcastreceiver. Java

 
Public class bootbroadcastreceiver extends broadcastreceiver {static final string action_boot = "android. intent. action. boot_completed "; @ overridepublic void onreceive (context, intent) {If (intent. getaction (). equals (action_boot) {intent ootstartintent = new intent (context, bootstartdemo. class); ootstartintent. addflags (intent. flag_activity_new_task); context. startactivity (ootstartintent );}}}

This class continues from broadcastreceiver. In the overwriting method onreceive, it detects whether the received intent meets
Boot_completed. If yes, start the activity bootstartdemo.

3. Configuration File

(1) androidmanifest. xml:

<? XML version = "1.0" encoding = "UTF-8"?> <! -- This is a boot self-starting program --> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" package = "com. ajie. bootstartdemo "Android: versioncode =" 1 "Android: versionname =" 1.0 "> <application Android: icon =" @ drawable/icon "Android: label = "@ string/app_name"> <activity Android: Name = ". bootstartdemo "Android: Label =" @ string/app_name "> <intent-filter> <action Android: Name =" android. intent. action. main "/> <category Android: Name =" android. intent. category. launcher "/> </intent-filter> </activity> <Cycler Android: Name = ". bootbroadcastreceiver "> <intent-filter> <action Android: Name =" android. intent. action. boot_completed "/> <category Android: Name =" android. intent. category. home "/> </intent-filter> </Cycler> </Application> <Uses-Permission Android: Name = "android. Permission. receive_boot_completed"> </uses-Permission>  </Manifest>

Note that the node registers a receiver with the system, and the sub-node intent-filter indicates receiving.
Android. Intent. Action. boot_completed message. Configure the permission for Android. Permission. receive_boot_completed.

(2) Layout file, Main. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <textview Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: text = "@ string/boottext" Android: textcolor = "#5f2dd2" Android: Background = "# ffffff" Android: textsize = "60px" Android: gravity = "center_horizontal"/> </linearlayout>

Compile the APK package and install it to the simulator or mobile phone. Shut down and reboot. The page showing the activity bootstartdemo is displayed.

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.