Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fatal Exception: java.lang.IllegalStateException - Not allowed to start service Intent (app is in background) #321

Open
ReneFerrari opened this issue Jan 25, 2022 · 2 comments
Labels

Comments

@ReneFerrari
Copy link

ReneFerrari commented Jan 25, 2022

DFU Bootloader version (please complete the following information):

  • SDK version: 31
  • Library version: 1.12.0

Device information (please complete the following information):

  • Device: Any device running Android 8 or higher

Describe the bug
From time to time we are facing this crash (Android 8 and up due to changes with services). Basically the App is in background and tries to start a service.

The affected LOC is: no.nordicsemi.android.dfu.DfuBaseService.onHandleIntent (DfuBaseService.java:1413)

(Note: It crashes on all startService calls, so not only the one I picked here)

I think changing it to:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    startForegroundService(intent);
} else {
    startService(intent);
}

would probably solve it.

Is there anything speaking against it?

I just checked for Android 12 and up there are unfortunately more restrictions: https://developer.android.com/about/versions/12/behavior-changes-12#foreground-service-launch-restrictions
So startForegroundService won't work there, rather WorkManager should be used

@junebeans
Copy link

To add a note - we need to add permission FOREGROUND_SERVICE on the consumer to start the foreground service. Here are the additional reference https://developer.android.com/guide/components/foreground-services#request-foreground-service-permissions. It would be nice if we have permission added in the library 👀

Ran this on v2.3.0 at android 13.

@philips77
Copy link
Member

So startForegroundService won't work there, rather WorkManager should be used

Indeed, WorkManager should be used. The library was created long before the first WorkManager existed in a way that it would have to be rewritten separating transport and runner.

You may also disable starting service as foreground at all by setting this to false:

public DfuServiceInitiator setForeground(final boolean foreground) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants