fabcira / neverEndingAndroidService

An example showing how to build a never ending background service in Android
63 stars 40 forks source link

Not working in oreo devices #1

Open aNnYvIrUs opened 5 years ago

aNnYvIrUs commented 5 years ago

Process: oak.shef.ac.uk.testrunningservicesbackgroundrelaunched, PID: 21844 java.lang.RuntimeException: Unable to start receiver oak.shef.ac.uk.testrunningservicesbackgroundrelaunched.SensorRestarterBroadcastReceiver: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=oak.shef.ac.uk.testrunningservicesbackgroundrelaunched/.SensorService }: app is in background uid UidRecord{5813532 u0a200 RCVR bg:+1m0s42ms idle procs:1 seq(0,0,0)} at android.app.ActivityThread.handleReceiver(ActivityThread.java:3399) at android.app.ActivityThread.-wrap18(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1780) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6944) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=oak.shef.ac.uk.testrunningservicesbackgroundrelaunched/.SensorService }: app is in background uid UidRecord{5813532 u0a200 RCVR bg:+1m0s42ms idle procs:1 seq(0,0,0)} at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1538) at android.app.ContextImpl.startService(ContextImpl.java:1484) at android.content.ContextWrapper.startService(ContextWrapper.java:663) at android.content.ContextWrapper.startService(ContextWrapper.java:663) at oak.shef.ac.uk.testrunningservicesbackgroundrelaunched.SensorRestarterBroadcastReceiver.onReceive(SensorRestarterBroadcastReceiver.java:16) at android.app.ActivityThread.handleReceiver(ActivityThread.java:3392) at android.app.ActivityThread.-wrap18(Unknown Source:0)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1780)  at android.os.Handler.dispatchMessage(Handler.java:105)  at android.os.Looper.loop(Looper.java:164)  at android.app.ActivityThread.main(ActivityThread.java:6944)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) 

jeroenbeuz commented 5 years ago

Same happens for me. However sometimes it does work. Will post any further findings here.

Saceone commented 5 years ago

Not working when app is swiped from recent apps list any solutions?

fabcira commented 5 years ago

It works on a Samsung S8 (SM-G950F) with Android 8.0. What phone causes a problem?

shofin-islam commented 5 years ago

Not working when app is swiped from recent apps list. (log is stuck) Why pressing back is calling service destroy()?

any solution?

CydeSwype commented 5 years ago

Same issue for me, but on Pie. OnePlus 5T OxygenOS 9.0.4 (Android version 9). Also happens on virtualized Nexus 5X. Trigger appears to be memory pressure (i.e. opening Maps app on the virtualized Nexus 5X is enough to usually cause Android to kill the background process).

CydeSwype commented 5 years ago

This summary from Android team makes it pretty clear that they're not trying to allow any workarounds for silent background processes that are long-running and reliable: https://developer.android.com/guide/background They consider battery and RAM impact top-priority issues for users' experience and that anything like a pedometer use case should be spawning a foreground service (so that a user knows that there could be battery and RAM impact and decide to uninstall the app). I don't think Android has done enough to explain to end-users that this was a big shift in the experience with the advent of Oreo. I still talk to users all the time that HATE sticky notifications and they don't appreciate that Android is trying to enforce transparency. They just want them gone (which forces us app makers to take on the burden of explaining how Android works to end-users). Short of Android team reversing their course on this policy, I think any workarounds for highly responsive background processes will be short-lived.