Android - 應用安裝、卸載、覆蓋安裝的廣播及不生效原因解析

版權聲明:本文爲博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/mythmayor/article/details/80653621
轉載請註明出處:https://blog.csdn.net/mythmayor/article/details/80653621

一、應用安裝、卸載、覆蓋安裝的廣播

最近想優化一下項目,因爲應用是有自動更新的功能的,想在覆蓋安裝的時候做一些自己的邏輯。於是使用了廣播來完成這個需求。
講到廣播接收者,大家都知道,Android中四大組件之一。我們需要新建一個類繼承BroadcastReceiver,然後記得去清單文件中配置一下廣播接收者。
代碼也非常簡單,首先看一下自定義的廣播接收者:

package com.mythmayor.appinstalllistener;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

/**
 * Created by mythmayor on 2018/6/11.
 */

public class MyInstallReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        //安裝廣播
        if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {
            String packageName = intent.getDataString();
            Toast.makeText(context, "安裝了應用:"+packageName, Toast.LENGTH_SHORT).show();
        }
        //卸載廣播
        if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) {
            String packageName = intent.getDataString();
            Toast.makeText(context, "卸載了應用:"+packageName, Toast.LENGTH_SHORT).show();
        }
        //覆蓋安裝廣播
        if (intent.getAction().equals("android.intent.action.PACKAGE_REPLACED")) {
            String packageName = intent.getDataString();
            Toast.makeText(context, "覆蓋安裝了應用:"+packageName, Toast.LENGTH_SHORT).show();
        }
    }
}

下面是清單文件的配置:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mythmayor.appinstalllistener">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RESTART_PACKAGES" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name=".MyInstallReceiver">
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_ADDED" />
                <action android:name="android.intent.action.PACKAGE_REMOVED" />
                <action android:name="android.intent.action.PACKAGE_REPLACED" />

                <data android:scheme="package" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

安裝應用和卸載應用的廣播就不說了。說一下覆蓋安裝的流程,實際上是先發送了卸載的廣播,然後才發送了覆蓋安裝的廣播。

二、不生效原因解析

開始我寫了個小Demo,模擬了一下這個流程,發現是沒有問題的。後來移接到項目上,發現接收不到廣播了。
後來想到,從線上更新下來的包是沒有這個廣播的。所以,尤其是使用覆蓋安裝的廣播時,一定要確認當前安裝包和要覆蓋的安裝包內有覆蓋安裝的廣播。結合上面說的覆蓋安裝的流程我們 很容易想到這一點,因爲覆蓋安裝時首先會把當前的安裝包卸載掉,如果要覆蓋的安裝包沒有相應的廣播接收者,那麼肯定就會接收不到廣播了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章