As you might know starting from Android 8 there are background execution limits. And there is another very popular stack overflow question about this topic. I came to this topic when I received an exception in my code. I have several broadcast receivers and when I catch them I start a service which does small amount of work on a background thread. I have moved that code out of broadcast receivers to make sure I don't block the UI thread and don't cause ANRs. However, sometimes the app will be in "background" mode and I will receive an exception
Java.Lang.IllegalStateException: Not allowed to start service Intent .... app is in background
Which is OK and expected due to background execution limits. However I can not reproduce this exception 100%, it happens only sometimes, most probably when OS decides that my app is no longer in background and kicks me out of the whitelist and I get this exception as soon as I try to start a service.
My question is - is there a adb command or developer option that will basically simulate the condition of app going to background and help to 100% reproduce this issue? I need this to debug several background usage scenarios and to make sure and prove app works as expected on all of the cases without guessing.