Skip to content

Conversation

@lydiym
Copy link

@lydiym lydiym commented Apr 3, 2022

Problem
Foreground Service doesn't work correctly on android O+. Here is trace:

04-03 15:27:21.277 24236 24236 E AndroidRuntime: FATAL EXCEPTION: main
04-03 15:27:21.277 24236 24236 E AndroidRuntime: Process: org.myapp.myapp, PID: 24236
04-03 15:27:21.277 24236 24236 E AndroidRuntime: java.lang.RuntimeException: Unable to start receiver org.myapp.myapp.TaskReceiver: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=org.myapp.myapp/.ServiceNotifier (has extras) }: app is in background uid UidRecord{64a6ed0 u0a301 RCVR idle change:idle|uncached procs:1 seq(0,0,0)}
04-03 15:27:21.277 24236 24236 E AndroidRuntime: 	at android.app.ActivityThread.handleReceiver(ActivityThread.java:4358)
04-03 15:27:21.277 24236 24236 E AndroidRuntime: 	at android.app.ActivityThread.access$2500(ActivityThread.java:296)
04-03 15:27:21.277 24236 24236 E AndroidRuntime: 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2238)
04-03 15:27:21.277 24236 24236 E AndroidRuntime: 	at android.os.Handler.dispatchMessage(Handler.java:107)
04-03 15:27:21.277 24236 24236 E AndroidRuntime: 	at android.os.Looper.loop(Looper.java:213)
04-03 15:27:21.277 24236 24236 E AndroidRuntime: 	at android.app.ActivityThread.main(ActivityThread.java:8178)
04-03 15:27:21.277 24236 24236 E AndroidRuntime: 	at java.lang.reflect.Method.invoke(Native Method)
04-03 15:27:21.277 24236 24236 E AndroidRuntime: 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
04-03 15:27:21.277 24236 24236 E AndroidRuntime: 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
04-03 15:27:21.277 24236 24236 E AndroidRuntime: Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=org.myapp.myapp/.ServiceNotifier (has extras) }: app is in background uid UidRecord{64a6ed0 u0a301 RCVR idle change:idle|uncached procs:1 seq(0,0,0)}
04-03 15:27:21.277 24236 24236 E AndroidRuntime: 	at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1720)
04-03 15:27:21.277 24236 24236 E AndroidRuntime: 	at android.app.ContextImpl.startService(ContextImpl.java:1675)
04-03 15:27:21.277 24236 24236 E AndroidRuntime: 	at android.content.ContextWrapper.startService(ContextWrapper.java:669)
04-03 15:27:21.277 24236 24236 E AndroidRuntime: 	at android.content.ContextWrapper.startService(ContextWrapper.java:669)
04-03 15:27:21.277 24236 24236 E AndroidRuntime: 	at org.myapp.myapp.ServiceNotifier.start(ServiceNotifier.java:18)
04-03 15:27:21.277 24236 24236 E AndroidRuntime: 	at org.myapp.myapp.TaskReceiver.onReceive(TaskReceiver.java:24)
04-03 15:27:21.277 24236 24236 E AndroidRuntime: 	at android.app.ActivityThread.handleReceiver(ActivityThread.java:4349)
04-03 15:27:21.277 24236 24236 E AndroidRuntime: 	... 8 more

Not allowed to start service Intent - it's about background service limitations: https://developer.android.com/about/versions/oreo/background

, But I use :foreground in buildozer.spec:

services = Notifier:notifier.py:foreground

Problem happens after app closing

There is related issue #1785

Fix
The fix repeats part of closed pull request https://github.com/kivy/python-for-android/pull/1786/files#diff-91249b27761ee10b96c07b64959a33951619a2547223ebeaaf82ece873e1e594R104

@mzakharo
Copy link
Contributor

mzakharo commented Jul 14, 2022

Interestingly, other bootstraps have this code already: https://github.com/kivy/python-for-android/blob/develop/pythonforandroid/bootstraps/service_library/build/templates/Service.tmpl.java#L41

Is there a reason why the common template has been kept different?

My suspicion is this is only an issue if you specify autorestart on the service:
PythonService.mService.setAutoRestartService(True).

I found that another (more robust) way to achieve autorestart functionality is to simply add :sticky to the service build parameter list. As a bonus, sticky will avoid the service being restarted on app-exit.

@kuzeyron kuzeyron added the core-providers Core code that's not a recipe label Apr 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core-providers Core code that's not a recipe

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants