According to the background execution limits introduced in Android Oreo, calling startService when the app is in background should throw an IllegalArgumentException
. Check this question: Android 8.0: java.lang.IllegalStateException: Not allowed to start service Intent.
I created a sample app targetting Android Oreo that does the following:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startService(new Intent(MainActivity.this, MyService.class));
}
}, 5000);
}
Then I start the app and after press the home button immediately bringing the app to the background state. But the exception is not thrown. How can it be? I expect the app to crash in this case.
According to Google's documentation on background service limitations:
Generally I've found the window to be something around a minute, but that can certainly be different for others.
Take a look at your overall device logs (run
adb logcat
from the command line or turn off filters in Android Studio's Logcat window) after you've backgrounded the app and look for something like:Any request to start a service after that should cause the exception. I'm guessing you'll need to up the timeout to something closer to a minute or two.