前面的文章 Android 開機廣播的使用 中已經提到Android的開機啟動,但是在Android 4.0 有時可以接收到開機啟動廣播,有時卻不可以。經過分析發現,如果應用程式安裝上始終沒有被開啟過,那麼在Android啟動時,該應用無法接收到開機啟動廣播android.permission.RECEIVE_BOOT_COMPLETED。
原來在Android 3.1的更新文檔中已經做了說明。
原文如下
Launch controls on stopped applications
Starting from Android 3.1, the system's package manager keeps track of applications that are in a stopped state and provides a means of controlling their launch from background processes and other applications.
Note that an application's stopped state is not the same as an Activity's stopped state. The system manages those two stopped states separately.
The platform defines two new intent flags that let a sender specify whether the Intent should be allowed to activate components in stopped application.
FLAG_INCLUDE_STOPPED_PACKAGES — Include intent filters of stopped applications in the list of potential targets to resolve against.
FLAG_EXCLUDE_STOPPED_PACKAGES — Exclude intent filters of stopped applications from the list of potential targets.
When neither or both of these flags is defined in an intent, the default behavior is to include filters of stopped applications in the list of potential targets.
Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents. It does this to prevent broadcasts from background services from inadvertently or unnecessarily launching components of stoppped applications. A background service or application can override this behavior by adding theFLAG_INCLUDE_STOPPED_PACKAGES flag to broadcast intents that should be allowed to activate stopped applications.
Applications are in a stopped state when they are first installed but are not yet launched and when they are manually stopped by the user (in Manage Applications).
翻譯如下
從Android 3.1開始,系統的軟體包管理器跟蹤處於停止狀態(stopped state)的應用程式,提供了一種控制其啟動後台進程和其他應用程式方式。
需要注意的是應用程式的停止狀態(stopped state)和Activity的停止狀態是不一樣的。該系統可以分別管理這兩種停止狀態。
該平台定義了兩個新的Intent的Flag,讓寄件者指定的意圖是否應該被允許啟用停止的應用程式的組件。
FLAG_INCLUDE_STOPPED_PACKAGES - Include intent filters of stopped applications in the list of potential targets to resolve against.包括停止的應用程式列表中的。
FLAG_EXCLUDE_STOPPED_PACKAGES - Exclude intent filters of stopped applications from the list of potential targets.排除停止的應用程式列表中的。
當兩個Flag都不設定或都設定的時候,預設操作是FLAG_INCLUDE_STOPPED_PACKAGES。
請注意,系統向所有的Intent的廣播添加了FLAG_EXCLUDE_STOPPED_PACKAGES標誌。它這樣做是為了防止廣播無意中的或不必要地開啟組件的stoppped應用程式的後台服務。後台服務或應用程式可以通過向廣播Intent添加FLAG_INCLUDE_STOPPED_PACKAGES標誌來喚醒處於停止狀態(stopped state)的應用程式。
應用程式處於停止狀態情況有兩種,一種是他們是第一次安裝,但尚未啟動,另一種是在管理應用程式中由使用者手動停止。