AltBeacon / android-beacon-library

Allows Android apps to interact with BLE beacons
Apache License 2.0
2.85k stars 837 forks source link

ForegroundServiceStartNotAllowedException Crash Despite Fallback Mechanism in v2.20.6 #1201

Open pubudithajayasekara opened 2 months ago

pubudithajayasekara commented 2 months ago

Issue Description

We are encountering a ForegroundServiceStartNotAllowedException in our application when using the org.altbeacon:android-beacon-library:2.20.6. According to the documentation, starting with library version 2.19.5-beta6, the library should handle this exception by falling back to using the Job Scheduler for scans when starting a foreground service is not allowed by the operating system. However, we are still experiencing crashes related to this issue.

Stack Trace

ForegroundServiceStartNotAllowedException: startForegroundService() not allowed due to mAllowStartForeground false ...

Expected Behavior

According to the documentation, the library should catch the ForegroundServiceStartNotAllowedException and use the Job Scheduler as a fallback, resuming the use of a foreground service when the app returns to the foreground.

Actual Behavior

Despite using version 2.20.6, the application is crashing due to the ForegroundServiceStartNotAllowedException, which suggests that the fallback mechanism to the Job Scheduler may not be working as intended.

Steps to Reproduce

  1. Integrate org.altbeacon:android-beacon-library:2.20.6 into the application.
  2. Attempt to start a foreground service while the app is in the background.
  3. Observe the crash related to ForegroundServiceStartNotAllowedException.

Additional Information

Documentation Reference

slai47 commented 1 month ago

Bumping this as this error is still occurring on 2.20.7.

Logs from Crashlytics from our production:

       at android.app.ActivityThread.handleCreateService(ActivityThread.java:4925)
       at android.app.ActivityThread.-$$Nest$mhandleCreateService()
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2407)
       at android.os.Handler.dispatchMessage(Handler.java:107)
       at android.os.Looper.loopOnce(Looper.java:232)
       at android.os.Looper.loop(Looper.java:317)
       at android.app.ActivityThread.main(ActivityThread.java:8592)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:878)
       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:5016)
       at android.os.Parcel.readParcelable(Parcel.java:4998)
       at android.os.Parcel.createExceptionOrNull(Parcel.java:3178)
       at android.os.Parcel.createException(Parcel.java:3167)
       at android.os.Parcel.readException(Parcel.java:3150)
       at android.os.Parcel.readException(Parcel.java:3092)
       at android.app.IActivityManager$Stub$Proxy.setServiceForeground(IActivityManager.java:6960)
       at android.app.Service.startForeground(Service.java:776)
       at org.altbeacon.beacon.service.BeaconService.startForegroundIfConfigured(BeaconService.java:279)
       at org.altbeacon.beacon.service.BeaconService.onCreate(BeaconService.java:201)
       at android.app.ActivityThread.handleCreateService(ActivityThread.java:4912)
       at android.app.ActivityThread.-$$Nest$mhandleCreateService()
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2407)
       at android.os.Handler.dispatchMessage(Handler.java:107)
       at android.os.Looper.loopOnce(Looper.java:232)
       at android.os.Looper.loop(Looper.java:317)
       at android.app.ActivityThread.main(ActivityThread.java:8592)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:878)

Android Version 14 is only effected for us. Replication steps are the same as above.

Devices are Samsung and Google. S24, 23 Ultra, Pixel 8, 7 and 6 for examples.

Version 2.20.6 on production, 2.20.7 on staging. Both having similar issues.

slai47 commented 1 month ago

Could this be related to https://issuetracker.google.com/issues/307329994 and needing to add in more permission settings with ServiceCompat?

oebb commented 2 weeks ago

We're also having the same issue w Android 14 devices (targeting Android 14)