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

Android 技術筆記:監聽應用的安裝與解除安裝廣播

編輯:關於 Android 開發

系統會主動送出的兩則廣播

在 Android 系統中,安裝與解除安裝都會發出對應的廣播,我們可以在自己的應用中攔截它們:

  • 應用安裝完成後,系統送出 android.intent.action.PACKAGE_ADDED。
  • 解除安裝時,系統送出 android.intent.action.PACKAGE_REMOVED。

兩者的套件名稱都可以透過 intent.getDataString() 取得(回傳的是 package: 開頭的 URI 字串,實務上通常會用 substring(8) 把前綴去掉,或直接改用 intent.getData().getSchemeSpecificPart() 更直觀)。拿到套件名之後,就能依需求做後續處理。

撰寫 BroadcastReceiver

新建一個繼承 BroadcastReceiver 的類別,在 onReceive 中依 action 分派處理:

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        // 開機完成後啟動程式
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            Intent newIntent = new Intent(context, xxxActivity.class);
            newIntent.setAction("android.intent.action.MAIN");
            newIntent.addCategory("android.intent.category.LAUNCHER");
            newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(newIntent);
        }

        // 有新應用安裝完成 → 自動啟動它
        if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {
            String packageName = intent.getDataString().substring(8);   // 去掉 "package:"
            System.out.println("---------------" + packageName);

            Intent newIntent = new Intent();
            newIntent.setClassName(packageName, packageName + ".MainActivity");
            newIntent.setAction("android.intent.action.MAIN");
            newIntent.addCategory("android.intent.category.LAUNCHER");
            newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(newIntent);
        }

        // 有應用被解除安裝 → 清理相關資料
        if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) {
            System.out.println("********************************");
            DatabaseHelper dbhelper = new DatabaseHelper();
            dbhelper.executeSql("delete from xxx");
        }
    }
}

從 Receiver 啟動 Activity 時,FLAG_ACTIVITY_NEW_TASK 是必須的,因為 Receiver 本身沒有 Activity 任務堆疊;而 intent.getAction() 建議改用 intent.getAction().equals(...) 反轉成 "...".equals(intent.getAction()) 的寫法,可以避免 action 為 null 時閃退。

修改 AndroidManifest.xml

<manifest package="app.ui" xmlns:android="https://schemas.android.com/apk/res/android">

    <receiver
        android:label="@string/app_name"
        android:name=".MyReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.PACKAGE_ADDED" />
            <action android:name="android.intent.action.PACKAGE_REMOVED" />
            <data android:scheme="package" />
            <!-- 注意!! 這句必須要加,否則收不到套件相關的廣播 -->
        </intent-filter>
    </receiver>

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.RESTART_PACKAGES" />
    <uses-permission android:name="android.permission.INTERNET" />
</manifest>

這裡有兩個關鍵點。第一,<data android:scheme="package" /> 千萬不能漏——套件安裝/移除類的廣播都帶有 package: 這個 data scheme,manifest 中沒有對應宣告時,即使 action 寫對了也收不到。第二,開機啟動需要 RECEIVE_BOOT_COMPLETED 權限,缺了它 BOOT_COMPLETED 永遠不會送達。

實務上的重要限制

這個範例能跑,但直接搬進正式專案會遇到幾個問題,值得先知道:

Android 3.1 之後的套件可見性限制。應用安裝完成時,PACKAGE_ADDED 只會送給已安裝的應用;而剛被安裝的新程式預設處於「未執行過」的停用狀態,所以範例中「新裝好就自動啟動它」的行為,在較新的系統版本上通常不會成功。

並非所有安裝都會觸發。透過 adb install 或大量同時安裝時,PACKAGE_ADDED 的收斂行為與一般使用者操作不同,不要依賴它作為「安裝計數」的準確來源。

Android 8.0 之後的背景啟動限制。從背景(尤其是 BroadcastReceiver)啟動 Activity 受到嚴格限制,直接 startActivity 往往會被系統攔下或丟上 Toast 提示。這類需求現在一律建議改用通知(Notification)引導使用者點擊進入。

onReceive 是同步的。它跑在主執行緒上且有約十秒的時限,範例中的資料庫操作若量大就該改用 goAsync() 或直接轉給 JobScheduler/WorkManager 處理,否則容易造成 ANR。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved