How to register for ACTION_PACKAGE_ADDED and ACTIO

2019-01-25 22:58发布

Looking at the latest Android Oreo release notes, it seems like only a handful of implicit broadcasts can be registered by the apps. ACTION_PACKAGE_ADDED and ACTION_PACKAGE_REMOVED is not among them. Is there a workaround for receiving these broadcasts?

1条回答
贼婆χ
2楼-- · 2019-01-25 23:55

From the docs:

Apps that target Android 8.0 or higher can no longer register broadcast receivers for implicit broadcasts in their manifest. An implicit broadcast is a broadcast that does not target that app specifically. For example, ACTION_PACKAGE_REPLACED is an implicit broadcast, since it is sent to all registered listeners, letting them know that some package on the device was replaced.

This says that you cannot register these intents in your manifest. You can still register them programmatically to receive them when your app is running.

You might also try ACTION_PACKAGE_FULLY_REMOVED, which is one of the exceptions that you can still listen to by registering it in the manifest. There is no such 'alternative' for when a package is added.

As CW noted, you could also periodically check for changes in the roster of installed apps.

You can also use polling, setting up a JobScheduler job to check every so often, asking PackageManager for what has changed in the roster of installed apps via getChangedPackages().

查看更多
登录 后发表回答