Android Question App crashes: Android 8.0 Background execution limits

Multiverse app

Active Member
Licensed User
Longtime User
One of my apps recently started crashing randomly, with no information in logs whatsoever. I have been debugging for 5 days now, and have no idea what is causing the issue. I have tried to debug every module, change targetSdkVersion, and recompile many times.
As the project is very big, I am not able to upload it here (and also for security purposes).

The minSdkVersion of project is 21 and targetSdkVersion is 27

I have uploaded the APK build here: https://drive.google.com/file/d/1WtgdWGSY0W_RizmROnJwRNeSDde8Q8dh/view?usp=sharing

Screenshot_20180105-152341.jpg


The crash reports in firebase show:

RuntimeException - ActivityThread.handleServiceArgs

Exception java.lang.RuntimeException: Unable to start service com.multiverse.jarvis.ttsservice@cdc6b0c with null: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.multiverse.jarvis/.starter }: app is in background uid UidRecord{e56f5b9 u0a705 SVC idle procs:1 seq(0,0,0)}

android.app.ActivityThread.handleServiceArgs (ActivityThread.java:4037)
android.app.ActivityThread.-wrap21 ()
android.app.ActivityThread$H.handleMessage (ActivityThread.java:2006)
android.os.Handler.dispatchMessage (Handler.java:108)
android.os.Looper.loop (Looper.java:166)
android.app.ActivityThread.main (ActivityThread.java:7396)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:245)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:926)

Caused by java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.multiverse.jarvis/.starter }: app is in background uid UidRecord{e56f5b9 u0a705 SVC idle procs:1 seq(0,0,0)}

android.app.ContextImpl.startServiceCommon (ContextImpl.java:1701)
android.app.ContextImpl.startService (ContextImpl.java:1657)
android.content.ContextWrapper.startService (ContextWrapper.java:644)
anywheresoftware.b4a.keywords.Common.StartService (Common.java:882)
anywheresoftware.b4a.objects.ServiceHelper$StarterHelper.onStartCommand (ServiceHelper.java:95)
com.multiverse.jarvis.ttsservice.onStartCommand (ttsservice.java:68)
android.app.ActivityThread.handleServiceArgs (ActivityThread.java:4009)
android.app.ActivityThread.-wrap21 ()
android.app.ActivityThread$H.handleMessage (ActivityThread.java:2006)
android.os.Handler.dispatchMessage (Handler.java:108)
android.os.Looper.loop (Looper.java:166)
android.app.ActivityThread.main (ActivityThread.java:7396)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:245)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:926)



The Google Developer console shows:


java.lang.IllegalStateException
anywheresoftware.b4a.keywords.Common.StartService

java.lang.RuntimeException:

at android.app.ActivityThread.handleServiceArgs (ActivityThread.java:4037)
at android.app.ActivityThread.-wrap21 (Unknown Source)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2006)
at android.os.Handler.dispatchMessage (Handler.java:108)
at android.os.Looper.loop (Looper.java:166)
at android.app.ActivityThread.main (ActivityThread.java:7396)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:245)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:926)

Caused by: java.lang.IllegalStateException:

at android.app.ContextImpl.startServiceCommon (ContextImpl.java:1701)
at android.app.ContextImpl.startService (ContextImpl.java:1657)
at android.content.ContextWrapper.startService (ContextWrapper.java:644)
at anywheresoftware.b4a.keywords.Common.StartService (Common.java:882)
at anywheresoftware.b4a.objects.ServiceHelper$StarterHelper.onStartCommand (ServiceHelper.java:95)
at com.multiverse.jarvis.ttsservice.onStartCommand (ttsservice.java:68)
at android.app.ActivityThread.handleServiceArgs (ActivityThread.java:4009)


I have attached the above-mentioned Starter, TTSservice modules, and AndroidManifest.

It would be grateful if this issue is solved.
Thank you.

Edit: Now I know the reason, just have to find a solution
 

Attachments

  • Starter.bas
    3.6 KB · Views: 542
  • TTSService.bas
    4.9 KB · Views: 591
  • AndroidManifest.xml
    9.7 KB · Views: 478
Last edited:

Multiverse app

Active Member
Licensed User
Longtime User
From the Error code, I found this answer: https://stackoverflow.com/questions...eexception-not-allowed-to-start-service-inten
The permitted situations are a temporary whitelist where the background service behaves the same as before Android O.

Under certain circumstances, a background app is placed on a temporary whitelist for several minutes. While an app is on the whitelist, it can launch services without limitation, and its background services are permitted to run. An app is placed on the whitelist when it handles a task that's visible to the user, such as:

  • Handling a high-priority Firebase Cloud Messaging (FCM) message.
  • Receiving a broadcast, such as an SMS/MMS message.
  • Executing a PendingIntent from a notification.
  • Starting a VpnService before the VPN app promotes itself to the foreground.
Source: https://developer.android.com/about/versions/oreo/background.html

So in other words if your background service does not meet the whitelist requirements you have to use the new JobScheduler. It's basically the same as a background service, but it gets called periodically instead of running in the background continuously.

In short, Apps that are running in the background now have limits on how freely they can access background services. I guess now any app targeting API level 26 or above, using background services will have to use jobscheduler?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Multiverse app

Active Member
Licensed User
Longtime User
Context.startForegroundService() method starts a foreground service. The system allows apps to call Context.startForegroundService() even while the app is in the background. However, the app must call that service's startForeground() method within five seconds after the service is created.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Relevant tutorial: https://www.b4x.com/android/forum/threads/87610/#content

You should set the targetSdkVersion to 25 for now.

You can no longer start services in the background when targetSdkVersion is 26+. This happens internally when you set StartAtBoot to true and in other cases.
StartForeground seems to be a bad practice for android and Google will fight against.
That's not accurate. You are allowed to start foreground services. You are not allowed to start non-foreground services when your app is not in the foreground.
However it requires calling Context.startForegroundService. This must be changed in B4A itself. It will be available soon (soon = probably next month).
 
Upvote 0
Top