Android 安装或删除其他应用程序时如何让我的应用程序接收广播
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11246326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make my app receive broadcast when other applications are installed or removed
提问by bmavus
I want to make an app that can receive broadcast when other apps on the device are installed or removed.
我想制作一个可以在安装或删除设备上的其他应用程序时接收广播的应用程序。
my code
我的代码
in manifset:
在清单中:
<receiver android:name=".apps.AppListener">
<intent-filter android:priority="100">
<action android:name="android.intent.action.PACKAGE_INSTALL"/>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
</intent-filter>
</receiver>
in AppListener:
在 AppListener 中:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class AppListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
// TODO Auto-generated method stub
Log.v(TAG, "there is a broadcast");
}
}
but I can't receive any broadcast. I think this problem is due to app permissions, any idea?
但是我收不到任何广播。我认为这个问题是由于应用程序权限造成的,知道吗?
Thanks for helps.
感谢您的帮助。
回答by t0mm13b
In your manifest:
在您的清单中:
<receiver android:name=".apps.AppListener">
<intent-filter android:priority="100">
<action android:name="android.intent.action.PACKAGE_INSTALL"/>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
</intent-filter>
</receiver>
Add the line before the intent-filter tag
在 intent-filter 标签之前添加一行
<data android:scheme="package"/>
So your manifest should look like this:
所以你的清单应该是这样的:
<receiver android:name=".apps.AppListener">
<intent-filter android:priority="100">
<action android:name="android.intent.action.PACKAGE_INSTALL"/>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
Am not sure about the PACKAGE_REMOVED intent in that if its actually is available.
如果它实际上可用,我不确定 PACKAGE_REMOVED 的意图。
回答by Heberth
You must eliminate android.intent.action.PACKAGE_INSTALLas it is deprecated and no longer recommended, because it is just for the system. Everything else is perfect and I would recommend that instead of 100, put 999, the documentation does not give maximum or minimum number to use, the larger the number, the higher priority will have your receiver for that intent. Sorry for the translator. I speak and write in Spanish. Information
您必须消除android.intent.action.PACKAGE_INSTALL因为它已被弃用且不再推荐,因为它仅适用于系统。其他一切都是完美的,我建议使用 999 而不是 100,文档没有给出要使用的最大或最小数量,数字越大,您的接收者对该意图的优先级就越高。对不起翻译。我用西班牙语说和写。 信息
<receiver android:name=".apps.AppListener">
<intent-filter android:priority="999">
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package"/>
</intent-filter>
回答by Gal Rom
Great Answers, just one small thing left:
很好的答案,只剩下一件小事:
On every App update first ACTION_PACKAGE_REMOVED will be called followed by ACTION_PACKAGE_ADDED- if you wish to ignore these events, just add it on your onReceive():
在每次应用更新时,首先调用 ACTION_PACKAGE_REMOVED,然后调用 ACTION_PACKAGE_ADDED - 如果您想忽略这些事件,只需将其添加到 onReceive() 中:
if(!(intent.getExtras() != null &&
intent.getExtras().containsKey(Intent.EXTRA_REPLACING) &&
intent.getExtras().getBoolean(Intent.EXTRA_REPLACING, false))) {
//DO YOUR THING
}
This is from the docs:
这是来自文档:
EXTRA_REPLACING Added in API level 3 String EXTRA_REPLACING Used as a boolean extra field in ACTION_PACKAGE_REMOVED intents to indicate that this is a replacement of the package, so this broadcast will immediately be followed by an add broadcast for a different version of the same package. Constant Value: "android.intent.extra.REPLACING"
EXTRA_REPLACING 在 API 级别 3 中添加字符串 EXTRA_REPLACING 用作 ACTION_PACKAGE_REMOVED 意图中的布尔额外字段,以指示这是包的替换,因此此广播将紧跟在同一包的不同版本的添加广播之后。常量值:“android.intent.extra.REPLACING”