Android Question Not allowed to start service Intent { cmp=appy.app/.starter }

FrankBerra

Active Member
Licensed User
Longtime User
Hello everyone!
I am experiencing random crashes on an app in which i am testing background services in Android 8

I created a service (called check) that executes some instructions every one minute:

B4X:
Sub Service_Start (StartingIntent As Intent)
    Log("start")
    Dim Salva As TextWriter
    Salva.Initialize(File.OpenOutput(File.DirDefaultExternal, "Test.txt", True))
    Salva.WriteLine(DateTime.Date(DateTime.Now) & " - " & DateTime.GetHour(DateTime.Now) & ":" & DateTime.GetMinute(DateTime.Now) & ":" & DateTime.GetSecond(DateTime.Now))
    Salva.close
   
    StartServiceAtExact(Me, DateTime.Now + 1000 * 60 * 1,True)
    Service.StopAutomaticForeground
End Sub

Randomly, let's say 2 or 3 times a day the app crashes with the following stacktrace:

B4X:
05-18 19:17:40.725 27303 27303 E AndroidRuntime: FATAL EXCEPTION: main
05-18 19:17:40.725 27303 27303 E AndroidRuntime: Process: appy.app, PID: 27303
05-18 19:17:40.725 27303 27303 E AndroidRuntime: java.lang.RuntimeException: Unable to start service appy.app.check@ec284f8 with null: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=appy.app/.starter }: app is in background uid UidRecord{db53f29 u0a255 SVC  idle procs:1 seq(0,0,0)}
05-18 19:17:40.725 27303 27303 E AndroidRuntime:     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3721)
05-18 19:17:40.725 27303 27303 E AndroidRuntime:     at android.app.ActivityThread.-wrap20(Unknown Source:0)
05-18 19:17:40.725 27303 27303 E AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1793)
05-18 19:17:40.725 27303 27303 E AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:105)
05-18 19:17:40.725 27303 27303 E AndroidRuntime:     at android.os.Looper.loop(Looper.java:164)
05-18 19:17:40.725 27303 27303 E AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:6809)
05-18 19:17:40.725 27303 27303 E AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
05-18 19:17:40.725 27303 27303 E AndroidRuntime:     at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
05-18 19:17:40.725 27303 27303 E AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
05-18 19:17:40.725 27303 27303 E AndroidRuntime: Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=appy.app/.starter }: app is in background uid UidRecord{db53f29 u0a255 SVC  idle procs:1 seq(0,0,0)}
05-18 19:17:40.725 27303 27303 E AndroidRuntime:     at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1522)
05-18 19:17:40.725 27303 27303 E AndroidRuntime:     at android.app.ContextImpl.startService(ContextImpl.java:1478)
05-18 19:17:40.725 27303 27303 E AndroidRuntime:     at android.content.ContextWrapper.startService(ContextWrapper.java:645)
05-18 19:17:40.725 27303 27303 E AndroidRuntime:     at anywheresoftware.b4a.keywords.Common.StartService(Common.java:884)
05-18 19:17:40.725 27303 27303 E AndroidRuntime:     at anywheresoftware.b4a.objects.ServiceHelper$StarterHelper.onStartCommand(ServiceHelper.java:211)
05-18 19:17:40.725 27303 27303 E AndroidRuntime:     at appy.app.check.onStartCommand(check.java:69)
05-18 19:17:40.725 27303 27303 E AndroidRuntime:     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3704)
05-18 19:17:40.725 27303 27303 E AndroidRuntime:     ... 8 more

Is there something that i can do for avoiding that crash?
 

DonManfred

Expert
Licensed User
Longtime User
Yes.

Only start the service every 15 to 30 minites. Android will prevent you app from starting in such short intervals
 
Upvote 0

FrankBerra

Active Member
Licensed User
Longtime User
Thank you for your precious sugegstions!
In this official page https://developer.android.com/about/versions/oreo/android-8.0-changes
i found this: "The startService() method now throws an IllegalStateException if an app targeting Android 8.0 tries to use that method in a situation when it isn't permitted to create background services."

So, when i am not permitted to create backgroud services?
Does the StartServiceAtExact in B4A uses startService() mentioned above or is it something different that should not throws an IllegalStateException?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

FrankBerra

Active Member
Licensed User
Longtime User
No, as you suggested i am not using that code anymore.
Hopefully when the service crashes it still keep going because it is a sticky one, but i can't handle the error in Starter's Sub Application_Error.

For example i use this code for catching the error:
B4X:
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Dim Salva As TextWriter
    Salva.Initialize(File.OpenOutput(File.DirDefaultExternal, "Test.txt", True))
    Salva.WriteLine("Crash")
    Salva.close
    Return False
End Sub
but nothing is written to the file and the default OS error message is displayed on the screen when app crashes even if i use "return false". At least is it possible to catch the exception in some way without showing error messages?

Thank you for your help!
 
Upvote 0

FrankBerra

Active Member
Licensed User
Longtime User
The attached example is crashing randomly as explained above and it doesn't catch the exception.
My device is a OnePlus3 with android 8.0.0. Maybe on your device the example is performing differently.
Take a look if you have time, thank you!
 

Attachments

  • TestBackgroudService.zip
    9.9 KB · Views: 314
Upvote 0

FrankBerra

Active Member
Licensed User
Longtime User
By removing #StartCommandReturnValue: android.app.Service.START_STICKY the service doesn't crash anymore.
So, should i abandon Sticky Services?
 
Upvote 0

FrankBerra

Active Member
Licensed User
Longtime User
Ok, thanks.
1) What happens if service crashes for other reasons? Is it going to start again if i set StartServiceAtExact before the crash?
2) On android 8 the OS kills the service quickly but what happens on previous versions? Should i continue to use sticky services?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
What happens if service crashes for other reasons? Is it going to start again if i set StartServiceAtExact before the crash?
If your process crashes many times then the OS will not start it at all. You should do the best to avoid such cases.

On android 8 the OS kills the service quickly but what happens on previous versions? Should i continue to use sticky services?
The background limits started with Android 6 and became more strict with each version.

You haven't explained why you used a sticky service.
 
Upvote 0

FrankBerra

Active Member
Licensed User
Longtime User
In my app, on regular intervals, i collect the position of users so I use sticky service because i need to keep the service running even in case of crash.
 
Upvote 0
Top