0

I've called startService in background.I run the application and send it to background immediately and I expect to see IllegalStateException after 10 seconds in android 8, but it works without any exception. In onCreate of activity:

  final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
          startService(new Intent(MainActivity.this, TestService.class));
        }
    }, 10000);
}

and in the service:

   @Override
protected void onHandleIntent(@Nullable Intent intent) {
    int i= 0;
    do {
        Log.i(TAG, "onHandleIntent: "+ i);
        i++;
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    } while (i<100);
}

gradle:

android {
compileSdkVersion 26
defaultConfig {
    applicationId "com.example.usergood.test"
    minSdkVersion 26
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
}
Amirhosein
  • 4,266
  • 4
  • 22
  • 35

1 Answers1

0

Have a look at: Oreo: startService() doesn't throw IllegalStateException when called in background

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.

Max Power
  • 169
  • 10