문제

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?

도움이 되었습니까?

해결책

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. */
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top