Android開機自啓動

Android開機自啓動程序

背景知識:當Android啓動時,會發出一個系統廣播,內容爲ACTION_BOOT_COMPLETED,它的字
符串常量表示爲 android.intent.action.BOOT_COMPLETED。只要在程序中「捕捉」到這個消息,再啓動之
便可。記住,Android框架說:Don''t call me, I''ll call you back。咱們要作的是作好接收這個消息的準備,而
實現的手段就是實現一個BroadcastReceiver。html

一、界面Activity,BootStartDemo.java文件java

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public   class   BootStartDemo extends   Activity {
     /** Called when the activity is first created. */
     @Override
     public   void   onCreate(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);
         new   Thread() {
             public   void   run() {
                 try   {
                     /*  10秒後關閉頁面*/
                     sleep( 10000 );
                 } catch   (Exception e) {
                     e.printStackTrace();
                 } finally   {
                     finish(); // 關閉頁面
                 }
             }
         }.start();
 
     }
}

這段代碼很簡單,當Activity 啓動時,會顯示TextView,用它顯示你想顯示的字樣,而且這個頁面只顯示10秒後消失。android

二、接收廣播消息:BootBroadcastReceiver.javaapp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 );
 //通過測試這個必定設置,啓動Activity的幾種方式之一,具體的本身搜索下
             ootStartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
             context.startActivity(ootStartIntent);
         }
 
     }
 
}

該類繼續自 BroadcastReceiver,覆載方法 onReceive 中,檢測接收到的 Intent 是否符合
BOOT_COMPLETED,若是符合,則啓動BootStartDemo這個Activity。框架

三、配置文件ide

(1)AndroidManifest.xml :測試

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?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>

注意其中顏色標紅那一部分,該節點向系統註冊了一個 receiver,子節點 intent-filter 表示接收
android.intent.action.BOOT_COMPLETED 消息。而且還要配置android.permission.RECEIVE_BOOT_COMPLETED權限。spa

(2)Layout文件,main.xmlcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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>

完成後,編譯出apk包,安裝到模擬器或手機中。關機,從新開機,就會顯示BootStartDemo這個Activity顯示出來的頁面。orm

相關文章
相關標籤/搜索