This weekend I decided to upgrade my old Android #nowplaying app. For the new version I decided to use Kotlin and also make it usable from my Android smartwatch. Honestly, it went much smoother than expected, except for one big caveat.
Out of some weird impulse, I decided to target the Oreo SDK (26). Both my phone and my watch are Oreo based, so that wouldn’t be a problem. Yet I kept running into the weird problem that my BroadcastReceivers wouldn’t receive any intents whatsoever. Interestingly enough, the WearableListenerService I used to to listen to Messages from the watch (I want to post when I tap a button on the watch) worked almost instantly.
Almost instantly, as in I had to enable Bluetooth debugging on my watch and my phone, then from the console run
adb forward tcp:4444 localabstract:/adb-hub and
adb connect 127.0.0.1:4444 every time I connect my device. (Checking the “ADB integration” setting in the menu of Android Studio is necessary too.)
I was using the source of Simple Last.fm Scrobbler for reference, since it’s doing exactly what I was trying to accomplish: catch the info of whatever music is playing. And it works on my phone no problem. Still, my BroadcastReceiver gets nothing. Even more interestingly, when I registered it dynamically at startup using registerReceiver (the way I was doing it in the old app), it worked just fine.
Of course that had the problem that it was a foreground process, so I’d have had to watch for all the lifecycle events and handle everything manually. Needless to say, much more trouble than I was willing to go to.
Then I googled the right thing: “BroadcastReceiver doesn’t work in Oreo.” This finally gave me the StackOverflow answer I needed so desperately. It turns out that in Android Oreo we can no longer listen to broadcasts that aren’t explicitly targeting our app.
I have no idea how to accomplish the same feat on Oreo if possible at all. Probably not. So I just downgraded to SDK 25 and as soon as I fixed some version numbers Android Studio wouldn’t fix for me, it worked fine. Talk about major versions and breaking APIs.