我希望用户安装或删除程序的时候,我并没有找到检测的BroadcastReceiver
才会这样。
I want to detect when the user installs or deletes an app, and I didn't find a BroadcastReceiver
that does this.
在我的应用程序,我得到的类 PackageManager
的安装的应用程序的信息,但我不希望定期扫描应用程序。是否有任何的BroadcastReceiver
这样做吗?或者有什么 ContentObserver
?是否有可能得到一个应用程序被安装或删除的通知?
In my app, I get the information about the installed apps with the class PackageManager
, but I don't want to scan the apps periodically. Is there any BroadcastReceiver
that does this? Or any ContentObserver
? Is it possible to get a notification that an app has been installed or removed?
您可以注册一个的BroadcastReceiver
使用 Intent.ACTION_PACKAGE_ADDED
(也是 Intent.ACTION_PACKAGE_REMOVED
和/或 Intent.ACTION_PACKAGE_CHANGED
如果需要的话)。例如,
You can register a BroadcastReceiver
using Intent.ACTION_PACKAGE_ADDED
(and also Intent.ACTION_PACKAGE_REMOVED
and/or Intent.ACTION_PACKAGE_CHANGED
if necessary). For instance,
void registerReceiver() {
IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
filter.addDataScheme("package_name");
}
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(Intent.ACTION_PACKAGE_ADDED)) {
Uri data = intent.getData();
String pkgName = data.getEncodedSchemeSpecificPart();
}
/* etc. */
}