4

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.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
makovkastar
  • 5,000
  • 2
  • 30
  • 50
  • Are you running the app on an Oreo device? – TheWanderer Sep 13 '18 at 15:21
  • @TheWanderer I'm running the app on a Pixel 2 with Android 9 (Pie) installed. – makovkastar Sep 13 '18 at 15:29
  • 1
    I think there is some timeout (maybe 15s). Try to set that delay to 20000. – egoldx Sep 13 '18 at 15:31
  • OK, so it should be happening. Can you point to where the docs say it should throw that Exception? – TheWanderer Sep 13 '18 at 15:31
  • @egoldx that's a different timeout. This Exception should apparently be thrown as soon as `startService()` is called. – TheWanderer Sep 13 '18 at 15:31
  • @TheWanderer https://developer.android.com/about/versions/oreo/android-8.0-changes, check the section Background execution limits: The startService() method now throws an IllegalStateException if an app targeting Android 8.0 tries to use that method in a situation when it isn't permitted to create background services. – makovkastar Sep 13 '18 at 15:33
  • Yup, but system still counts that app as foreround. – egoldx Sep 13 '18 at 15:33
  • @egoldx oh true. That might be why. – TheWanderer Sep 13 '18 at 15:34
  • @makovkastar Are you sure your app is in background when `startService` is fired? – egoldx Sep 13 '18 at 15:35
  • @egoldx I set the timeout first to 20s, then to 30s, still doesn't crash. Yes, I press the home button right after the app has started and wait for 30 seconds. – makovkastar Sep 13 '18 at 15:36
  • Found something interesting. Let me write up an answer. – TheWanderer Sep 13 '18 at 15:38
  • Actually, nevermind, I need to keep looking. – TheWanderer Sep 13 '18 at 15:42
  • I'm puzzled reading the question and comments. I also faced the crash on Oreo. I haven't updated to pie though. – exploitr Sep 13 '18 at 15:54
  • I don't have enough for an answer unfortunately, but I do have the links for people to trace: https://github.com/aosp-mirror/platform_frameworks_base/blob/master/core/java/android/app/ContextImpl.java#L1549, https://raw.githubusercontent.com/aosp-mirror/platform_frameworks_base/master/services/core/java/com/android/server/am/ActivityManagerService.java, https://raw.githubusercontent.com/aosp-mirror/platform_frameworks_base/master/services/core/java/com/android/server/am/ActiveServices.java. A diff between `master` and `oreo-release` might help. – TheWanderer Sep 13 '18 at 16:02
  • is your app targeting 26 or < SDK? – Darshan Sep 13 '18 at 17:38
  • @DarShan targetSdkVersion 27 – makovkastar Sep 14 '18 at 07:38

1 Answers1

3

According to Google's documentation on background service limitations:

While an app is in the foreground, it can create and run both foreground and background services freely. When an app goes into the background, it has a window of several minutes in which it is still allowed to create and use services.

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:

09-26 13:25:37.150 4741 4756 W ActivityManager: Stopping service due to app idle: u0a267 -1m12s700ms com.example/.MyService

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.

Sean
  • 31
  • 2