Android boot auto-Start Program details

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. Just "capture" the message in the program and start it again
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
Copy codeThe Code is as follows: public class BootStartDemo extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
// No title
RequestWindowFeature (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 {
/* Close the page after 8 seconds */
Sleep (10000 );
} Catch (Exception e ){
E. printStackTrace ();
} Finally {
Finish (); // close the page
}
}
}. Start ();

}
}

This code is very simple. When the Activity is started, TextView is displayed, and the words you want to display are displayed, and the page disappears after 10 seconds.
2. Receive broadcast messages: BootBroadcastReceiver. java
Copy codeThe Code is as follows: 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 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:
Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<! -- This is a self-starting program -->
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "com. ajie. bootstartdemo"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
<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>
<SPAN style = "COLOR: # ff00ff"> <receiver android: name = ". BootBroadcastReceiver">
<Intent-filter>
<Action android: name = "android. intent. action. BOOT_COMPLETED"/>
<Category android: name = "android. intent. category. HOME"/>
</Intent-filter>
</Cycler>
</SPAN> </application>
<SPAN style = "COLOR: # ff00ff"> <STRONG> <uses-permission android: name = "android. permission. RECEIVE_BOOT_COMPLETED"> </uses-permission> </STRONG>
</SPAN> </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
Copy codeThe Code is as follows: <? 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.