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

[Android] Error: Context.startForegroundService() did not then call Service.startForeground() #1874

Closed
ernestbies opened this issue Jan 12, 2023 · 9 comments
Labels
Bug Platform: Android Unconfirmed Bugs that have not been confirmed by the core team.

Comments

@ernestbies
Copy link

ernestbies commented Jan 12, 2023

Describe the Bug
This problem has been present in the library for a very long time but is still not fixed and all topics related to it are closed. So I decided to create a new one.

Topics related to this issue: #473, #524, #1676.

The problem is serious, has been present for several years and affects the user experience of users using the react-native-track-player library.

Steps To Reproduce
Lock the screen when building the app. In addition, the problem occurs after listening to a given track for a long period of time.
The problem also occurs when users use the application (data from Crashlytics, reported by users).

  1. Run the application via react-native run-android and make a screen lock.
  2. An error will occur.
Fatal Exception: android.app.RemoteServiceException$ForegroundServiceDidNotStartInTimeException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{1fac5c6 u0 com.exampleapp.app/com.doublesymmetry.trackplayer.service.MusicService}
       at android.app.ActivityThread.generateForegroundServiceDidNotStartInTimeException(ActivityThread.java:2242)
       at android.app.ActivityThread.throwRemoteServiceException(ActivityThread.java:2213)
       at android.app.ActivityThread.-$$Nest$mthrowRemoteServiceException()
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2505)
       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:8741)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)```

Another way to trigger the problem is to replace startService() with startForegroundService() in the file MusicModule.kt. Wait about 10 seconds and after this change application will crash with the same error.

Replicable on Example App?
Yes.

Environment Info:
The problem occurs on physical devices and emulators. Versions:
"react-native": "^0.70.6"
"react-native-track-player": "^3.2.0"

@ernestbies ernestbies added the Bug label Jan 12, 2023
@daniel-plewinski
Copy link

same here - since the problem is reported by so many developers maybe it's time to take action

@PatrykGodlewski
Copy link

Encountering the same problem, I found that there are limitations on apps in the background. Locking the screen, while building the app makes that app as background, in that case, it cant run invoked before on build services. More about these limitations here https://developer.android.com/about/versions/oreo/background

@jspizziri
Copy link
Collaborator

maybe it's time to take action

@daniel-plewinski I encourage you to do exactly that! That's the beauty of open source, you're the master of your own destiny. I'll happily review any PRs that you or any other people on this thread submit.

@jspizziri jspizziri added Platform: Android Unconfirmed Bugs that have not been confirmed by the core team. labels Jan 12, 2023
@jspizziri
Copy link
Collaborator

jspizziri commented Jan 12, 2023

@ernestbies I encourage you to make a fork of this project an implement a concise repro of your issue there (in the example app).

@ernestbies
Copy link
Author

ernestbies commented Jan 12, 2023

@jspizziri

Hey,

thanks for answer. In the attached topics, the developers were looking for various solutions, but unfortunately none of them solved the problem.

Related error:

Fatal Exception: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.exampleapp.app/com.guichaguri.trackplayer.service.MusicService }: app is in background uid UidRecord{bf8b0 u0a226 LAST bg:+1m11s396ms idle change:cached procs:1 seq(0,0,0)}
       at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1715)
       at android.app.ContextImpl.startService(ContextImpl.java:1670)
       at android.content.ContextWrapper.startService(ContextWrapper.java:720)
       at android.content.ContextWrapper.startService(ContextWrapper.java:720)
       at com.guichaguri.trackplayer.module.MusicModule.waitForConnection(MusicModule.java:108)
       at com.guichaguri.trackplayer.module.MusicModule.getQueue(MusicModule.java:415)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:372)
       at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:151)
       at com.facebook.react.bridge.queue.NativeRunnable.run(NativeRunnable.java)
       at android.os.Handler.handleCallback(Handler.java:938)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:27)
       at android.os.Looper.loop(Looper.java:223)
       at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:226)
       at java.lang.Thread.run(Thread.java:923)

I updated the issue title and content to be more readable for users.

@ernestbies ernestbies changed the title [Android] Error: Not allowed to start service Intent [Android] Error: Context.startForegroundService() did not then call Service.startForeground() Jan 12, 2023
@martinmidtsund
Copy link
Contributor

I think this will be solved by #1888

@elliotdickison
Copy link
Contributor

I think this will be solved by #1888

I can confirm this. Down from 20-30 crashes a day to 0. Thank you!

@ernestbies
Copy link
Author

@martinmidtsund thanks! :-)

@nl-danish
Copy link

is this fixed? This error is getting logged the most in crashanalytics, I was thinking if there's a work around or if the issue has been fixed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Platform: Android Unconfirmed Bugs that have not been confirmed by the core team.
Projects
None yet
Development

No branches or pull requests

7 participants