如何正确的过滤包取代广播如何正确

2023-09-05 10:07:31 作者:龙飞冲天

我试图抓住包取代广播我的应用程序,只有我的应用程序,但由于某些原因,在我的reciever我是广播的每一个应用程序的更新。我还以为你只需要在设置清单文件到您的应用程序的意图过滤器,但也许我错了吗?

下面是我的code(清单):

 <接收机器人:名称=。UpdateReciever>
        <意向滤光器>
            <作用机器人:名称=android.intent.action.PACKAGE_REPLACED/>
            <数据机器人:计划=包机器人:路径=com.my.app/>
        &所述; /意图滤光器>
    < /接收器>
 

Reciever:

 公共类AppUpdateReciever扩展的BroadcastReceiver {

    @覆盖
    公共无效的onReceive(上下文CON,意图意图){

        // code ..
    }

}
 
石化污水深度处理工艺

解决方案

添加到您的onReceive方法:

 如果(intent.getDataString()。包括(com.my.app)){
    ...
}
 

编辑: 请注意,在注册 ACTION_PACKAGE_REPLACED 使你的应用程序在每次任何应用程序更新时启动,如果不是已经打开。我不知道该怎么API 12日之前,以避免这种情况,但在12 API,你可以注册 ACTION_MY_PACKAGE_REPLACED ,所以你不必过滤的意图和你的应用程序获得了 T为其他应用程序更新不必要的启动。

I am trying to catch the package replaced broadcast for my app and only my app, but for some reason in my reciever I am the broadcast for every app that is updated. I thought you only needed to set the intent filter in the manifest file to your app, but maybe I am wrong?

Here's my code(manifest):

        <receiver android:name=".UpdateReciever">
        <intent-filter>
            <action android:name="android.intent.action.PACKAGE_REPLACED" />
            <data android:scheme="package" android:path="com.my.app" />
        </intent-filter>
    </receiver>

Reciever:

public class AppUpdateReciever extends BroadcastReceiver {

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

        //code..    
    }

}

解决方案

Add this to your onReceive method:

if (intent.getDataString().contains("com.my.app")){
    ...
}

EDIT: Note that registering for ACTION_PACKAGE_REPLACED causes your app to be started up every time any app is updated, if it wasn't already open. I don't know how to avoid this before API 12, but in API 12 you can register for ACTION_MY_PACKAGE_REPLACED so you don't have to filter the intent and your app won't be started unnecessarily by other apps being updated.