Background: When Android starts, a system broadcast will be issued, the content is action_boot_completed, its word
The string constant is expressed as android.intent.action.BOOT_COMPLETED. Just "snap" to this message in the program and start the
Can. Keep in mind that the Android framework says, "Don't call me, I'll be back." What we have to do is prepare to receive the message, and
The means of realization is to realize a broadcastreceiver.
1. Interface Activity,bootstartdemo.java File
|
publicclassBootStartDemo extendsActivity { /** Called when the activity is first created. */ @Override publicvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 无title requestWindowFeature(Window.FEATURE_NO_TITLE); // 全屏 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.main); newThread() { public voidrun() { try{ /* 10秒后关闭页面*/ sleep(10000); } catch(Exception e) { e.printStackTrace(); } finally{ finish(); // 关闭页面 } } }.start(); }} |
|
|
This code is very simple, when the activity starts, will display the TextView, uses it to display the typeface which you want to display, and this page only displays 10 seconds disappears.
2. Receive broadcast message: Bootbroadcastreceiver.java
publicclassBootBroadcastReceiver extendsBroadcastReceiver { staticfinalString action_boot="android.intent.action.BOOT_COMPLETED"; @Override publicvoidonReceive(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); } }} |
|
|
|
The class continues to be self-broadcastreceiver, in the onreceive of the method, to detect whether the received Intent conforms to the
Boot_completed, if compliant, initiates bootstartdemo this activity.
3. Configuration files
(1) Androidmanifest.xml:
|
<?xml version="1.0"encoding="utf-8"?><!-- 这是一个开机自启动程序 --><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> <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> </receiver></span> </application><span style="color: #ff00ff;"><strong><uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission></strong></span></manifest> |
|
|
Note that the color is red that part of the node registers a receiver with the system, and the child node Intent-filter indicates the receiving
Android.intent.action.BOOT_COMPLETED message. And also configure Android.permission.RECEIVE_BOOT_COMPLETED permissions.
(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> |
|
|
|
When you are done, compile the APK package and install it in the emulator or phone. Shutdown, reboot, will show Bootstartdemo this activity shows the page.
Android Boot up program