0

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.

kyurkchyan
  • 2,260
  • 2
  • 23
  • 37
  • I don't thing there is a command for that . but you can limit the emulator memory and force it by opening other apps other than that you can assume you have the condition and just start where the exception happens or throw one your self for debugging. but the life-cycle of android is pretty clear and i think you can code based on that. – Alireza Sharifi Jun 15 '19 at 04:23
  • 1
    also there is some settings in the development option of device(bottom of the list) to set limited of background apps. didn't tried it yet but i think if you put it on 1 and open another two apps , your app will be terminated. – Alireza Sharifi Jun 15 '19 at 04:32
  • @lvl4fi4 thanks for the reply. Yeah I am going with that route now, which is cumbersome and slow. I wish there was other way. Regarding the background apps in the developer settings, that makes sense I'll try it out. – kyurkchyan Jun 16 '19 at 13:41

0 Answers0