如何检测在Android设备的新应用 [英] How to detect new apps in an Android device

查看:185
本文介绍了如何检测在Android设备的新应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望用户安装或删除程序的时候,我并没有找到检测的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. */
}

这篇关于如何检测在Android设备的新应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆