How to detect new apps in an Android device

老子叫甜甜 提交于 2019-12-04 14:02:51

问题


I want to detect when the user installs or deletes an app, and I didn't find a BroadcastReceiver that does this.

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?


回答1:


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


来源:https://stackoverflow.com/questions/11487637/how-to-detect-new-apps-in-an-android-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!