Android Question Copilot questions StartService

JackKirk

Well-Known Member
Licensed User
Longtime User
I am involved in a lengthy dialog with Copilot to develop a B4A app that has a service that survives the aggressive service kill characteristics of Samsung S23 Ultra (and I assume other similar phones).

Part of that dialog:
Question:
=========

doesn't:
'Start Monitor_XUpload service
Private wrk_jo_context As JavaObject
wrk_jo_context.InitializeContext
Private wrk_intent As JavaObject
wrk_intent.InitializeNewInstance("android.content.Intent", Array(wrk_jo_context, Monitor_XUpload))
Try
wrk_jo_context.RunMethod("startForegroundService", Array(wrk_intent))
Catch
Log("Main: startForegroundService failed: " & LastException.Message)
End Try
--- do what b4a does with:
StartService(Monitor_XUpload)

Response:
=========

You're spot on to compare them, Jack—they both start a service, but the underlying system behavior differs significantly, especially on Android 8+.
Let’s break it down:

🧠 StartService(Monitor_XUpload) in B4A
• Internally calls context.startService(intent)
• Works fine on Android < 8
• On Android 8+:
• If the service doesn’t call startForeground() within 10 seconds, the system throws RemoteServiceException and kills it
• This is not safe for foreground services unless you're absolutely sure the service will promote itself immediately

🧠 Your JavaObject version with startForegroundService

wrk_jo_context.RunMethod("startForegroundService", Array(wrk_intent))

- Internally calls context.startForegroundService(intent)
- Required on Android 8+ when starting a service that will call startForeground()
- Gives the system a heads-up: “this service will promote itself”
- Avoids the 10-second crash window if your service posts a notification promptly

✅ So Yes—Functionally Similar, But Behaviorally Safer
Your version does what StartService(...) does, but with explicit compliance for foreground services on Android 8+. It’s the correct choice when:
- You’re starting a service that will immediately call startForeground()
- You want to avoid system-level crashes on newer Android versions
- You’re dealing with Samsung or Pixel firmware that aggressively enforces foreground rules
Is this true or is Copilot hallucinating?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
A better implementation of StartForegroundService is available in the background location example: https://www.b4x.com/android/forum/threads/background-location-tracking.99873/#content

B4X:
Private Sub StartForegroundService(Service As Object)
    Dim p As Phone
    If p.SdkVersion <= 26 Then
        StartService(Service)
        Return
    End If
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    Dim intent As JavaObject
    intent.InitializeNewInstance("android.content.Intent", Array(ctxt, Service))
    ctxt.RunMethod("startForegroundService", Array(intent))
End Sub

It is needed there because the service is started from a receiver.

Assuming that your app is running and visible, and only then you start the service, then you don't need it.

You still need to declare the foreground service type. For example (B4A-Bridge):
B4X:
SetServiceAttribute(Service1, android:foregroundServiceType, connectedDevice)
 
Upvote 0
Top