Android 接收安装, 卸载,更新等系统广播

本文介绍如何使用`BroadcastReceiver`监听Android应用的安装、卸载和更新事件。通过创建`InstallationReceiver`类并动态或静态注册,可以实现对系统动作的响应。在`onCreate()`中设置接收器,`onDestroy()`中解除注册,确保正确接收和处理ACTION_PACKAGE_ADDED、ACTION_PACKAGE_REMOVED和ACTION_PACKAGE_REPLACED等广播。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有时我们要对安装,卸载,更新等系统动作进行处理

  这时我们可以使用广播接收器BroadcastReceiver,作为Android的四大组件
  大家也是非常的熟悉了,直接上代码:

/**
* 安装广播
*
* @author Administrator
*/
public class InstallationReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // 接收安装广播
    if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED") || Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())) {
        String packageName = intent.getDataString().split(":")[1];
        // ToolUtils.showToast(context, "安装了:" +packageName + "包名的程序");
        Logout.log(getClass().getSimpleName(), "安装了:" + packageName + "包名的程序");
        DownloadManager downloadManager = DownloadService.getDownloadManager(context);
        if (null != downloadManager) {
            DownloadInfo info2 = downloadManager.getTaskByPackgeName(packageName);
            if (info2 != null) {
                Logout.log(getClass().getSimpleName(), "安装时的downloadinfo:" + info2);
                info2.setIsInstall(true);
                if (info2.isCover()) {
                    info2.setIsCover(false);
                }
                downloadManager.appStatus(info2);
                downloadManager.deleteFileByTask(context,info2.getTargetPath());
                DownloadManager.openApk(context, packageName);

            }


        }


    }
    // 接收卸载广播
    if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) {
        String packageName = intent.getDataString().split(":")[1];
        // ToolUtils.showToast(context, "卸载了:" + packageName + "包名的程序");
        Logout.log(getClass().getSimpleName(), "卸载了:" + packageName + "包名的程序");
        DownloadManager downloadManager = DownloadService.getDownloadManager(context);
        if (null != downloadManager) {
            DownloadInfo info2 = downloadManager.getTaskByPackgeName(packageName);
            Logout.log(getClass().getSimpleName(), "卸载时的安装信息:" + info2);
            if (info2 != null && !info2.isCover()) {//不是覆盖安装 是直接卸载
                Logout.log(getClass().getSimpleName(), "卸载时的downloadinfo:" + info2);
                info2.setState(DownloadManager.NONE);
                info2.setIsInstall(false);
                info2.setDownloadLength(0);// 接受到卸载的广播
                downloadManager.appStatus(info2);
                downloadManager.removeTaskFromDisk(info2.getUrl());
            }


        }
    }
}

}

只要是四大组件都必须注册
1. 静态注册 (直接在AndroidManifest中添加的)

    <receiver android:name=".broadcastReceiver.DownloadReceiver"
        android:label="@string/app_name">
        <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"/>
            <!--这行代码很关键,如果scheme没有指定,那其它的属性均无效-->
            <data android:scheme="package"/>
        </intent-filter>
    </receiver>

2. 动态注册
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_download_manager);
downloadManager = DownloadService.getDownloadManager();
allTask = downloadManager.getAllTask();
ListView listView = (ListView) findViewById(R.id.listView);
adapter = new MyAdapter();
listView.setAdapter(adapter);
downloadManager.getThreadPool().getExecutor().addOnAllTaskEndListener(this);
setupReceiver();
}

// 设置安装下载广播
private void setupReceiver() {
downloadReceiver =new DownloadReceiver() ;
IntentFilter intentFilter=new IntentFilter();
intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED); // 安装
intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); // 卸载
intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED); // 更新
intentFilter.addDataScheme(“package”);
DownloadManagerActivity.this.registerReceiver(downloadReceiver,intentFilter);
}
@Override
protected void onDestroy() {
super.onDestroy();
//记得移除,否者会回调多次
downloadManager.getThreadPool().getExecutor().removeOnAllTaskEndListener(this);
// 别忘了解除绑定
if(null!=downloadReceiver){
unregisterReceiver(downloadReceiver);
}
}

通过以上代码大家就可以对安装,卸载,更新等动作进行相应的操作

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值