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

LocationUpdatesService.startTracking -> android.app.ForegroundServiceStartNotAllowedException #66

Open
autokus opened this issue Oct 13, 2023 · 7 comments

Comments

@autokus
Copy link

autokus commented Oct 13, 2023

First of all, thank you for this plugin, on my experiance it works very well.

But I see such exceptions in the Google Play console. I can't reproduce this and I have no idea how I can fix it.

Exception java.lang.RuntimeException:
at android.app.ActivityThread.handleCreateService (ActivityThread.java:5086)
at android.app.ActivityThread.-$$Nest$mhandleCreateService
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2428)
at android.os.Handler.dispatchMessage (Handler.java:106)
at android.os.Looper.loopOnce (Looper.java:226)
at android.os.Looper.loop (Looper.java:313)
at android.app.ActivityThread.main (ActivityThread.java:8762)
at java.lang.reflect.Method.invoke
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:604)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1067)
Caused by android.app.ForegroundServiceStartNotAllowedException:
at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel (ForegroundServiceStartNotAllowedException.java:54)
at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel (ForegroundServiceStartNotAllowedException.java:50)
at android.os.Parcel.readParcelableInternal (Parcel.java:4787)
at android.os.Parcel.readParcelable (Parcel.java:4755)
at android.os.Parcel.createExceptionOrNull (Parcel.java:3018)
at android.os.Parcel.createException (Parcel.java:3007)
at android.os.Parcel.readException (Parcel.java:2990)
at android.os.Parcel.readException (Parcel.java:2932)
at android.app.IActivityManager$Stub$Proxy.startService (IActivityManager.java:6214)
at android.app.ContextImpl.startServiceCommon (ContextImpl.java:1967)
at android.app.ContextImpl.startForegroundService (ContextImpl.java:1933)
at android.content.ContextWrapper.startForegroundService (ContextWrapper.java:839)
at com.icapps.background_location_tracker.service.LocationUpdatesService.startTracking (LocationUpdatesService.java)
at com.icapps.background_location_tracker.service.LocationUpdatesService.onCreate (LocationUpdatesService.java)
at android.app.ActivityThread.handleCreateService (ActivityThread.java:5073)

Devices / OS:
samsung xcoverpro (Android 12 (SDK 31))
samsung r9q (Android 13 (SDK 33))
samsung a42xq (Android 12 (SDK 31))
samsung dm3q (Android 13 (SDK 33))

Lib-Version: 1.4.1

Flutter-Version: 3.13.x (not 100% sure but I think 3.13.5)

@ikbendewilliam
Copy link
Contributor

Hi autokus, I haven't seen this before but I'll check with different projects (cc @luca-vz).
Two questions about this, do you only run on Samsung (probably not, but we have encountered errors before on Samsung that didn't happen on any other devices, so I'm wary of Samsung-only errors).
Do you still receive data or can you tell that the error is blocking the user experience?

@ikbendewilliam
Copy link
Contributor

I looked a little further into it and indeed we also have the error in our firebase. Note that it's not only a Samsung issue, but happens more often.
What the error says is correct, you can't start a background thread when you are in the background. And that is what we are seeing in our crashlytics, all error were thrown when the app was in the background. We had around 3000 sessions and 17 people encountered it. No reports of any issue. So as far as I can see the error is never thrown when a user is actively using the app and therefore not really an issue. We will look further into it and if I encounter anything interesting I'll let you know.

@ikbendewilliam
Copy link
Contributor

ikbendewilliam commented Oct 20, 2023

Some more info about the error: https://stackoverflow.com/questions/70044393/fatal-android-12-exception-startforegroundservice-not-allowed-due-to-mallows

Apps that target Android 12 (API level 31) or higher can't start foreground services while running in the background, except for a few special cases. If an app tries to start a foreground service while the app is running in the background, and the foreground service doesn't satisfy one of the exceptional cases, the system throws a ForegroundServiceStartNotAllowedException.

Exemptions from background start restrictions

In the following situations, your app can start foreground services even while your app is running in the background:

  • Your app transitions from a user-visible state, such as an activity.
  • Your app can start an activity from the background, except for the case where the app has an activity in the back stack of an existing task.
  • Your app receives a high-priority message using Firebase Cloud Messaging.
  • The user performs an action on a UI element related to your app. For example, they might interact with a bubble, notification, widget, or activity.
  • Your app invokes an exact alarm to complete an action that the user requests.
  • Your app is the device's current input method.
  • Your app receives an event that's related to geofencing or activity recognition transition.
  • After the device reboots and receives the ACTION_BOOT_COMPLETED, ACTION_LOCKED_BOOT_COMPLETED, or ACTION_MY_PACKAGE_REPLACED intent action in a broadcast receiver.

For more info please check link1 link2

@autokus
Copy link
Author

autokus commented Oct 20, 2023

Hi ikbendewilliam

Thank you for your attention on this issue. A week ago we uploaded an app upgrade with some lib and build lib upgrades:

gradle-wrapper.properies:
gradle-7.3.3-all.zip -> gradle-7.5-all.zip

build.gradle:
com.android.tools.build:gradle:7.2.2 -> com.android.tools.build:gradle:7.4.2
com.google.gms:google-services:4.3.10 -> com.google.gms:google-services:4.3.15

I don't know if it's because of that, but with the new version of our app, we haven't had any error reported by Google so far.

I will inform you if we see the error again.

@ikbendewilliam
Copy link
Contributor

Ah good to know! Thank you for sharing!

@autokus
Copy link
Author

autokus commented Oct 27, 2023

Hi ikbendewilliam

The exception has unfortunately occurred again. However, only 2x on a samsung r9q (Galaxy S21 FE 5G), Android 13 (SDK 33).

I did not specify any permission for foreground and background service in my AndroidManifest.xml. I saw that this was done in the lib itself. Am I correct there, I do not need to specify anything? In the doc for the plugin it says something about iOS configuration but nothing about Android.

@ikbendewilliam
Copy link
Contributor

Hi @autokus
If I look into a project where we use this, I do see the following permissions in my manifest 🤔 Note that we do also ask the REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission.
However in the app that I've mentioned before, we also receive said error and we do specify the permissions. So this probably won't really effect things.

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />

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

No branches or pull requests

2 participants