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?
2 Answers
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().
- 41,901
- 18
- 127
- 145
-
1You 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()`: https://developer.android.com/reference/android/content/pm/PackageManager.html#getChangedPackages(int) – CommonsWare Sep 01 '17 at 10:14
-
@CommonsWare do you have any working example for getChangedPackages()? It is very much confusing with sequenceNumber parameter which will be reset to zero after the reboot. – venkat Mar 01 '18 at 07:22
-
1@venkat: [This sample app](https://github.com/commonsguy/cw-omnibus/tree/v8.10/Introspection/SAWMonitor) monitors for new/changed apps and yells about those that it finds that request the `SYSTEM_ALERT_WINDOW` permission. – CommonsWare Mar 01 '18 at 11:45
-
@CommonsWare Thanks a lot. I am looking into this. – venkat Mar 11 '18 at 18:43
-
`You can still register them programmatically to receive them when your app is running`. Do you have an example? – Nolesh Mar 19 '18 at 12:20
-
2I tried to register a broadcast from a service which is running in the foreground, but nothing is working. I can listen to the SCREEN_ON and SCREEN_OFF, but not the PACKAGE_ADDED or PACKAGE_REMOVED – Fossor Aug 14 '18 at 17:24
As posted in https://stackoverflow.com/a/55819091/1848826
I could make it work with the following code
class KotlinBroadcastReceiver(action: (context: Context, intent: Intent) -> Unit) : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) = action(context, intent) } class MainActivity : AppCompatActivity() { private val broadcastReceiver = KotlinBroadcastReceiver { context, _ -> Toast.makeText(context, "It works", Toast.LENGTH_LONG).show() } override fun onCreate(savedInstanceState: Bundle?) { registerReceiver(broadcastReceiver, IntentFilter().apply { addAction(Intent.ACTION_PACKAGE_ADDED) addAction(Intent.ACTION_PACKAGE_REMOVED) addDataScheme("package") // I could not find a constant for that :( }) } override fun onDestroy() { super.onDestroy() unregisterReceiver(broadcastReceiver) } }
- 853
- 2
- 10
- 20