Android開機(jī)自啟動(dòng)程序詳解
更新時(shí)間:2013年06月19日 10:44:20 作者:
本篇文章是對(duì)Android開機(jī)自啟動(dòng)程序進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
背景知識(shí):當(dāng)Android啟動(dòng)時(shí),會(huì)發(fā)出一個(gè)系統(tǒng)廣播,內(nèi)容為ACTION_BOOT_COMPLETED,它的字
符串常量表示為 android.intent.action.BOOT_COMPLETED。只要在程序中“捕捉”到這個(gè)消息,再啟動(dòng)之
即可。記住,Android框架說:Don''t call me, I''ll call you back。我們要做的是做好接收這個(gè)消息的準(zhǔn)備,而
實(shí)現(xiàn)的手段就是實(shí)現(xiàn)一個(gè)BroadcastReceiver。
1、界面Activity,BootStartDemo.java文件
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 {
/* 8秒后關(guān)閉頁(yè)面*/
sleep(10000);
} catch (Exception e) {
e.printStackTrace();
} finally {
finish(); // 關(guān)閉頁(yè)面
}
}
}.start();
}
}
這段代碼很簡(jiǎn)單,當(dāng)Activity 啟動(dòng)時(shí),會(huì)顯示TextView,用它顯示你想顯示的字樣,并且這個(gè)頁(yè)面只顯示10秒后消失。
2、接收廣播消息:BootBroadcastReceiver.java
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);
}
}
}
該類繼續(xù)自 BroadcastReceiver,覆載方法 onReceive 中,檢測(cè)接收到的 Intent 是否符合
BOOT_COMPLETED,如果符合,則啟動(dòng)BootStartDemo這個(gè)Activity。
3、配置文件
(1)AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<!-- 這是一個(gè)開機(jī)自啟動(dòng)程序 -->
<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>
注意其中顏色標(biāo)紅那一部分,該節(jié)點(diǎn)向系統(tǒng)注冊(cè)了一個(gè) receiver,子節(jié)點(diǎn) intent-filter 表示接收
android.intent.action.BOOT_COMPLETED 消息。并且還要配置android.permission.RECEIVE_BOOT_COMPLETED權(quán)限。
(2)Layout文件,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>
完成后,編譯出apk包,安裝到模擬器或手機(jī)中。關(guān)機(jī),重新開機(jī),就會(huì)顯示BootStartDemo這個(gè)Activity顯示出來的頁(yè)面。
符串常量表示為 android.intent.action.BOOT_COMPLETED。只要在程序中“捕捉”到這個(gè)消息,再啟動(dòng)之
即可。記住,Android框架說:Don''t call me, I''ll call you back。我們要做的是做好接收這個(gè)消息的準(zhǔn)備,而
實(shí)現(xiàn)的手段就是實(shí)現(xiàn)一個(gè)BroadcastReceiver。
1、界面Activity,BootStartDemo.java文件
復(fù)制代碼 代碼如下:
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 {
/* 8秒后關(guān)閉頁(yè)面*/
sleep(10000);
} catch (Exception e) {
e.printStackTrace();
} finally {
finish(); // 關(guān)閉頁(yè)面
}
}
}.start();
}
}
這段代碼很簡(jiǎn)單,當(dāng)Activity 啟動(dòng)時(shí),會(huì)顯示TextView,用它顯示你想顯示的字樣,并且這個(gè)頁(yè)面只顯示10秒后消失。
2、接收廣播消息:BootBroadcastReceiver.java
復(fù)制代碼 代碼如下:
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);
}
}
}
該類繼續(xù)自 BroadcastReceiver,覆載方法 onReceive 中,檢測(cè)接收到的 Intent 是否符合
BOOT_COMPLETED,如果符合,則啟動(dòng)BootStartDemo這個(gè)Activity。
3、配置文件
(1)AndroidManifest.xml :
復(fù)制代碼 代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<!-- 這是一個(gè)開機(jī)自啟動(dòng)程序 -->
<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>
注意其中顏色標(biāo)紅那一部分,該節(jié)點(diǎn)向系統(tǒng)注冊(cè)了一個(gè) receiver,子節(jié)點(diǎn) intent-filter 表示接收
android.intent.action.BOOT_COMPLETED 消息。并且還要配置android.permission.RECEIVE_BOOT_COMPLETED權(quán)限。
(2)Layout文件,main.xml
復(fù)制代碼 代碼如下:
<?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包,安裝到模擬器或手機(jī)中。關(guān)機(jī),重新開機(jī),就會(huì)顯示BootStartDemo這個(gè)Activity顯示出來的頁(yè)面。
您可能感興趣的文章:
- android開機(jī)自啟動(dòng)原理與實(shí)現(xiàn)案例(附源碼)
- Android的權(quán)限設(shè)置及自啟動(dòng)設(shè)置方法
- android開機(jī)自啟動(dòng)apk的方法
- android開機(jī)自啟動(dòng)APP及使用adb命令測(cè)試方法
- Android 代碼設(shè)置開機(jī)自啟動(dòng)App的方法
- Android開機(jī)自啟動(dòng)服務(wù)的實(shí)現(xiàn)方法
- android開機(jī)自啟動(dòng)app示例分享
- Android應(yīng)用實(shí)現(xiàn)安裝后自啟動(dòng)的方法
- android引導(dǎo)用戶開啟自啟動(dòng)權(quán)限的方法
- android studio開發(fā)實(shí)現(xiàn)APP開機(jī)自啟動(dòng)
相關(guān)文章
Android 自定義對(duì)話框 showSetPwdDialog
這篇文章主要介紹了Android 自定義對(duì)話框 showSetPwdDialog的相關(guān)資料,需要的朋友可以參考下2016-03-03
Android資源文件與層次式導(dǎo)航超詳細(xì)講解
這篇文章主要介紹了Android資源文件與層次式導(dǎo)航,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-12-12
Android實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
Android組件之DrawerLayout實(shí)現(xiàn)抽屜菜單
DrawerLayout組件同樣是V4包中的組件,也是直接繼承于ViewGroup類,所以這個(gè)類也是一個(gè)容器類。接下來通過本文給大家介紹Android組件之DrawerLayout實(shí)現(xiàn)抽屜菜單,感興趣的朋友一起學(xué)習(xí)吧2016-02-02
android操作SQLite增刪改減實(shí)現(xiàn)代碼
android操作SQLite增刪改減實(shí)現(xiàn)代碼,學(xué)習(xí)android的朋友可以參考下。2010-11-11
Android的權(quán)限設(shè)置及自啟動(dòng)設(shè)置方法
今天小編就為大家分享一篇Android的權(quán)限設(shè)置及自啟動(dòng)設(shè)置方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07

