3

Before Android 8.0, Oreo, you could detect a headset plugged in with a background service with a registered broadcast. With Android O's background execution limits, you can no longer do this without a notification.

While ACTION_HEADSET_PLUG is an excepted broadcast, it still doesn't send to Manifest registered receivers.

So you cannot use a background service. And you cannot be woken up via a manifest receiver. What's the solution? Never update your targetSDKVersion?

Justin
  • 3,322
  • 2
  • 22
  • 37
  • 2
    As I think this is an overlooked repercussion of Android 8.0, I've filed an issue with the framework team. https://issuetracker.google.com/issues/65672608 – Justin Sep 14 '17 at 16:33

1 Answers1

0

Seems, by official documentation, you can use ACTION_HEADSET_PLUG with no Android O limitation, because it is in the list of the exempted broadcasts:

Note: A number of implicit broadcasts are currently exempted from this limitation. Apps can continue to register receivers for these broadcasts in their manifests, no matter what API level the apps are targeting. For a list of the exempted broadcasts, see Implicit Broadcast Exceptions.

UPDATE:

But IRL, as CommonsWare wrote, there are only

intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);

flag for ACTION_HEADSET_PLUG and not Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND flag as, for example, for ACTION_LOCALE_CHANGED.

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79