Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 4.0 收不到開機廣播?問題出在 stopped state 機制

編輯:關於 Android 程式設計

在〈Android 開機廣播的使用〉一文中已經談過開機自啟的做法,但升到 Android 4.0 之後會出現一個很令人困惑的現象:同一支 App,有時候能收到 android.intent.action.BOOT_COMPLETED,有時候完全沒反應。

真正的原因

仔細比對會發現規律:如果 App 安裝完成後從來沒有被手動打開過一次,開機時它就收不到 RECEIVE_BOOT_COMPLETED 廣播。這不是 bug,而是 Android 3.1 就已經公告的行為變更。

官方文件怎麼說

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 applications.

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 stopped applications. A background service or application can override this behavior by adding the FLAG_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).

白話整理

  1. 從 Android 3.1 起,PackageManager 會追蹤處於「已停止(stopped state)」的 App,並提供機制控制它能否被背景行程或其他 App 喚起。
  2. 要注意:App 的 stopped state 與 Activity 的 stopped state 是兩回事,系統分別管理,不要混淆。
  3. 平台定義了兩個新的 Intent 旗標:
    • FLAG_INCLUDE_STOPPED_PACKAGES:把處於 stopped state 的 App 的 intent-filter 也納入解析候選。
    • FLAG_EXCLUDE_STOPPED_PACKAGES:把這些 App 排除在候選之外。
  4. 兩者都不設或兩者都設時,預設行為等同 FLAG_INCLUDE_STOPPED_PACKAGES。
  5. 關鍵:系統會對所有廣播 Intent 自動加上 FLAG_EXCLUDE_STOPPED_PACKAGES,目的是避免背景服務的廣播意外地、不必要地喚起那些已被停止的 App。若你確實需要喚醒,必須自己在發送端加上 FLAG_INCLUDE_STOPPED_PACKAGES 覆蓋這個行為。

哪些情況會進入 stopped state

  • App 第一次安裝完成、但從未啟動過。
  • 使用者在「設定 → 應用程式 → 強制停止」中手動停止。

對開發者的實際影響與因應

這解釋了為什麼你的開機自啟「在自己機器上可以、使用者裝完卻不行」——因為開發時你早就開過 App 了,它不在 stopped state;而使用者剛裝完還沒點開,於是廣播被系統攔下。

幾種常見因應方式:

  • 在首次啟動時引導使用者開啟一次,或於安裝後由 Play 商店的自動更新觸發一次啟動(新版本安裝會解除 stopped 狀態)。
  • 改用 JobScheduler / WorkManager 搭配 setRequiresDeviceIdle 這類由系統代為喚起的機制,避免依賴高風險的自啟手段。
  • 若使用第三方推送服務,注意其 SDK 是否已處理此限制;直接監聽 BOOT_COMPLETED 在新版 Android 上會越來越不可靠,Android 8.0 之後更對背景啟動有嚴格限制。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved