'Wear OS ANR on broadcast act=android.intent.action.SCREEN_OFF

I'm getting regular ANR's on my WearOS app regarding the android.intent.action.SCREEN_OFF broadcast, which I haven't registered for.

It happens primarily on Samsung Galaxy Watch4 devices, like the one I am developing on.

The case is complicated and googling didn't help much. Here is the flow, bear with me:

  1. The app launches normally, works as expected;
  2. The watch is turned downwards and the screen goes off. So far so good;
  3. I wait about 20 seconds for the Wear OS to move my app to the background and return to the watch face;
  4. I see in ADB that the app is running;
  5. I turn the watch up, the screen turns on and I see the watch face, my app is in the background;
  6. I turn the watch back down, wait a few seconds and then it crashes with ANR

Reason: Broadcast of Intent { act=android.intent.action.SCREEN_OFF flg=0x50200010 (has extras) }

I've been developing for a while and I know how to read the logs, but I found nothing there that could lead me to what registers for this broadcast and does not respond. The only possible reason I can think of is the imports I am using, which are scarce. Here they are:

dependencies {
    implementation 'com.google.android.gms:play-services-wearable:17.1.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.recyclerview:recyclerview:1.2.1'
    implementation 'androidx.wear:wear:1.2.0'
    implementation 'androidx.core:core-splashscreen:1.0.0-beta01'
    implementation 'androidx.preference:preference:1.2.0'
    implementation 'com.mcxiaoke.volley:library:1.0.19'
    implementation 'androidx.viewpager2:viewpager2:1.0.0'
}

SDK:

        minSdk 25
        targetSdk 31

I did suspect the splashscreen as closest to the screen and removed it, but the ANR happened nevertheless.

I even tried implementing my own BroadcastReceiver that does not unregister on onPause in order to give some answer to the system, even added this.goAsync().finish(); to its onReceive(), but it didn't help.

Has anyone bumped into this? Any help will be greatly appreciated!



Solution 1:[1]

UPDATE 2022-04-24:

After the last OTA update for the Galaxy 4 watches, version FVC8, the issue seems to be resolved. Keeping this here for historical reasons while versions prior to FVC8 are still active.


A few hours later, and after Yuri Schimke's comment I understood that there is a bug report to Google exactly for this.

https://issuetracker.google.com/issues/220190983

I was able to circumvent the issue by implementing a BroadcastReceiver for BOTH Intent.ACTION_SCREEN_OFF AND Intent.ACTION_SCREEN_ON that does nothing but logging the event and calling this.goAsync().finish();. This BroadcastReceiver is registered on onCreate and unregistered on onDestroy.

This way the ANR does NOT happen anymore, though I'm not yet certain as of what effect it has on the battery.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1