【问题标题】:How to detect new apps in an Android device如何检测 Android 设备中的新应用
【发布时间】:2012-07-14 07:35:57
【问题描述】:

我想检测用户何时安装或删除应用程序,但我没有找到这样做的BroadcastReceiver

在我的应用程序中,我使用 PackageManager 类获取有关已安装应用程序的信息,但我不想定期扫描应用程序。有没有这样做的BroadcastReceiver?或任何ContentObserver?是否可以收到应用程序已安装或删除的通知?

【问题讨论】:

  • 我相信答案是 ...
  • 看来我错了,:-),我忘记了!
  • 如果你有雄心壮志,你应该看看AsyncTaskLoader 课程......在我看来,在这里实现你自己的Loader 将是一次非常好的学习体验:P
  • 文档中的 sample code 实际上完全符合您的描述(或者至少我相信它确实如此,从它的外观来看)。看看吧!
  • 谢谢大家。我不知道我会怎么做,但是我想在应用列表更改时将数据上传到服务器,而不需要在那一刻运行应用,所以我想我会用服务来做,是吗最好的方法是什么? :)

标签: android broadcastreceiver contentobserver android-package-managers


【解决方案1】:

您可以使用Intent.ACTION_PACKAGE_ADDED 注册BroadcastReceiver(如有必要,还可以使用Intent.ACTION_PACKAGE_REMOVED 和/或Intent.ACTION_PACKAGE_CHANGED)。例如,

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. */
}

【讨论】:

  • 我认为Intent.ACTION_PACKAGE_CHANGEDIntent.ACTION_PACKAGE_ADDEDIntent.ACTION_PACKAGE_REMOVED 做的一样,不是吗?
  • 没有。您可以从这里查看:developer.android.com/reference/android/content/…
  • 谢谢,我正在寻找“getEncodedSchemeSpecificPart”,我原以为它是我需要的 getHost,但 uri 中缺少 // 打破了这一点...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-11
  • 2011-11-22
  • 1970-01-01
  • 1970-01-01
  • 2018-02-21
  • 1970-01-01
相关资源
最近更新 更多