Android Question Starter Service Crashing

skaliwag

Member
Licensed User
Longtime User
The Developer Concole gives this as an issue that needs attention :-

Missing foreground service type
From Android 14, developers must specify service types when foreground services are found in their app's manifest. A version of your app that is currently in production is crashing because it tries to start a foreground service when no foreground service type is declared. Make sure that service types for the services that start in the foreground are specified in your app's manifest.

The stack for the crash shows this

Exception java.lang.RuntimeException:
at android.app.ActivityThread.handleServiceArgs (ActivityThread.java:5286)
at android.app.ActivityThread.-$$Nest$mhandleServiceArgs
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2531)
at android.os.Handler.dispatchMessage (Handler.java:106)
at android.os.Looper.loopOnce (Looper.java:230)
at android.os.Looper.loop (Looper.java:319)
at android.app.ActivityThread.main (ActivityThread.java:8919)
at java.lang.reflect.Method.invoke
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:578)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1103)
Caused by java.lang.RuntimeException:
at anywheresoftware.b4a.objects.ServiceHelper$StarterHelper.handleStartIntent (ServiceHelper.java:173)
at uk.co.stefsquared.vanguard.starter.handleStart (starter.java:102)
at uk.co.stefsquared.vanguard.starter.access$000 (starter.java:8)
at uk.co.stefsquared.vanguard.starter$1.run (starter.java:74)
at anywheresoftware.b4a.objects.ServiceHelper$StarterHelper.onStartCommand (ServiceHelper.java:240)
at uk.co.stefsquared.vanguard.starter.onStartCommand (starter.java:72)
at android.app.ActivityThread.handleServiceArgs (ActivityThread.java:5268)
Caused by android.app.MissingForegroundServiceTypeException:
at android.app.MissingForegroundServiceTypeException$1.createFromParcel (MissingForegroundServiceTypeException.java:53)
at android.app.MissingForegroundServiceTypeException$1.createFromParcel (MissingForegroundServiceTypeException.java:49)
at android.os.Parcel.readParcelableInternal (Parcel.java:4882)
at android.os.Parcel.readParcelable (Parcel.java:4864)
at android.os.Parcel.createExceptionOrNull (Parcel.java:3064)
at android.os.Parcel.createException (Parcel.java:3053)
at android.os.Parcel.readException (Parcel.java:3036)
at android.os.Parcel.readException (Parcel.java:2978)
at android.app.IActivityManager$Stub$Proxy.setServiceForeground (IActivityManager.java:7234)
at android.app.Service.startForeground (Service.java:775)
at anywheresoftware.b4a.objects.ServiceHelper.StartForeground (ServiceHelper.java:85)
at anywheresoftware.b4a.objects.ServiceHelper$StarterHelper.handleStartIntent (ServiceHelper.java:171)

This is a B4A project built with Version 13 targeting SDK 34
I have Service.StopAutomaticForeground in the Service_Start Sub of Starter

As is so often the case, I am unable to duplicate the error
Any help much appreciated
 
Last edited:

agraham

Expert
Licensed User
Longtime User
 
Upvote 0

skaliwag

Member
Licensed User
Longtime User
Thank you for that, but I don't see how I can use it to fix this problem.

It seems to be the Starter service itself that is crashing
The crash dump says that ServiceHelper.StarterHelper.handleStartIntent is not providing a foreground service type
 
Upvote 0

ppgirl

Member
Licensed User
Longtime User
Are you calling Service.StartForeground?

- No. Don't use any service (only starter). It only use FirebaseMessaging with the receiver.

Is there any service that is registered to start in the background?

- No.

There are Android 14 devices with the issue , <1% devices reported by Play console -

iQOO I2208 (iQOO Z6 Lite 5G)
Android 14 (SDK 34)
Version: 39 (1.3.9)Occurred: 7 days ago


Infinix Infinix-X6711 (Infinix NOTE 30 5G)
Android 14 (SDK 34)
Version: 61 (1.6.1)Occurred: 7 days ago


iQOO I2301 (iQOO Z7 Pro 5G)
Android 14 (SDK 34)
Version: 61 (1.6.1)Occurred: 10 days ago


OPPO OP56E1L1 (CPH2455)
Android 14 (SDK 34)
Version: 65 (1.6.5)Occurred: 17 days ago

I have tested the Samsung and Pixel Android 14 in firebase's Testlab, but did not duplicate the issue.
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I am having the same issue with the default Starter service (https://www.b4x.com/android/forum/threads/starter-service-crash-anr.163162/)

This is the crash I get:
Exception java.lang.RuntimeException: at android.app.ActivityThread.handleServiceArgs (ActivityThread.java:5286) at android.app.ActivityThread.-$$Nest$mhandleServiceArgs at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2531) at android.os.Handler.dispatchMessage (Handler.java:106) at android.os.Looper.loopOnce (Looper.java:230) at android.os.Looper.loop (Looper.java:319) at android.app.ActivityThread.main (ActivityThread.java:8934) at java.lang.reflect.Method.invoke at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:578) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1103) Caused by java.lang.RuntimeException: at anywheresoftware.b4a.objects.ServiceHelper$StarterHelper.handleStartIntent (ServiceHelper.java:173) at com.BOBs.BBS.starter.handleStart (starter.java:102) at com.BOBs.BBS.starter.access$000 (starter.java:8) at com.BOBs.BBS.starter$1.run (starter.java:74) at anywheresoftware.b4a.objects.ServiceHelper$StarterHelper.onStartCommand (ServiceHelper.java:240) at com.BOBs.BBS.starter.onStartCommand (starter.java:72) at android.app.ActivityThread.handleServiceArgs (ActivityThread.java:5268) Caused by android.app.MissingForegroundServiceTypeException: at android.app.MissingForegroundServiceTypeException$1.createFromParcel (MissingForegroundServiceTypeException.java:53) at android.app.MissingForegroundServiceTypeException$1.createFromParcel (MissingForegroundServiceTypeException.java:49) at android.os.Parcel.readParcelableInternal (Parcel.java:4882) at android.os.Parcel.readParcelable (Parcel.java:4864) at android.os.Parcel.createExceptionOrNull (Parcel.java:3064) at android.os.Parcel.createException (Parcel.java:3053) at android.os.Parcel.readException (Parcel.java:3036) at android.os.Parcel.readException (Parcel.java:2978) at android.app.IActivityManager$Stub$Proxy.setServiceForeground (IActivityManager.java:7234) at android.app.Service.startForeground (Service.java:775) at anywheresoftware.b4a.objects.ServiceHelper.StartForeground (ServiceHelper.java:85) at anywheresoftware.b4a.objects.ServiceHelper$StarterHelper.handleStartIntent (ServiceHelper.java:171:
Exception java.lang.RuntimeException:
  at android.app.ActivityThread.handleServiceArgs (ActivityThread.java:5286)
  at android.app.ActivityThread.-$$Nest$mhandleServiceArgs
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2531)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loopOnce (Looper.java:230)
  at android.os.Looper.loop (Looper.java:319)
  at android.app.ActivityThread.main (ActivityThread.java:8934)
  at java.lang.reflect.Method.invoke
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:578)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1103)
Caused by java.lang.RuntimeException:
  at anywheresoftware.b4a.objects.ServiceHelper$StarterHelper.handleStartIntent (ServiceHelper.java:173)
  at com.BOBs.BBS.starter.handleStart (starter.java:102)
  at com.BOBs.BBS.starter.access$000 (starter.java:8)
  at com.BOBs.BBS.starter$1.run (starter.java:74)
  at anywheresoftware.b4a.objects.ServiceHelper$StarterHelper.onStartCommand (ServiceHelper.java:240)
  at com.BOBs.BBS.starter.onStartCommand (starter.java:72)
  at android.app.ActivityThread.handleServiceArgs (ActivityThread.java:5268)
Caused by android.app.MissingForegroundServiceTypeException:
  at android.app.MissingForegroundServiceTypeException$1.createFromParcel (MissingForegroundServiceTypeException.java:53)
  at android.app.MissingForegroundServiceTypeException$1.createFromParcel (MissingForegroundServiceTypeException.java:49)
  at android.os.Parcel.readParcelableInternal (Parcel.java:4882)
  at android.os.Parcel.readParcelable (Parcel.java:4864)
  at android.os.Parcel.createExceptionOrNull (Parcel.java:3064)
  at android.os.Parcel.createException (Parcel.java:3053)
  at android.os.Parcel.readException (Parcel.java:3036)
  at android.os.Parcel.readException (Parcel.java:2978)
  at android.app.IActivityManager$Stub$Proxy.setServiceForeground (IActivityManager.java:7234)
  at android.app.Service.startForeground (Service.java:775)
  at anywheresoftware.b4a.objects.ServiceHelper.StartForeground (ServiceHelper.java:85)
  at anywheresoftware.b4a.objects.ServiceHelper$StarterHelper.handleStartIntent (ServiceHelper.java:171)
 
Last edited:
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
This is what Googles Gemini says:

B4X:
Gemini may display inaccurate information, so double-check its responses
Summary

The app is crashing because it's trying to start a foreground service without providing a foreground service type. This is happening in the handleStartIntent method of the ServiceHelper class from the anywheresoftware.b4a.objects package, which is likely a library used for building the app. The crash occurs when the StartForeground method is called without the necessary foreground service type. This is a new requirement in recent Android versions, and the app needs to be updated to handle it.
Potential Solutions

    Specify a Foreground Service Type:

    The most likely cause of the crash is that the StartForeground method is being called without a foreground service type. To fix this, you need to provide a valid foreground service type when calling this method.

    For example, if your service is playing audio in the background, you would use the FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK type:

    // Assuming 'context' is available within the 'handleStartIntent' method
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        ServiceHelper.StartForeground(context, 1, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK);
    } else {
        ServiceHelper.StartForeground(context, 1, notification);
    }

    Make sure to choose the appropriate foreground service type based on the functionality of your service. You can find a list of available types in the Android documentation for ServiceInfo.

    Check for Older Android Versions:

    It's possible that the ServiceHelper library you are using doesn't handle the foreground service type requirement correctly for older Android versions. You can add a version check before calling StartForeground to ensure that the type is only provided on Android versions that require it (Android 10 and above).

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        // Call StartForeground with the appropriate type
    } else {
        // Call StartForeground without the type
    }

    This approach ensures backward compatibility while addressing the crash on newer Android versions.

Is this helpful?

The problem is in B4X we don't actually call the Starter?
 
Upvote 0

Enxix

Member
Hello people, good.
I have some doubts, I have read the information on the forum regarding the services in the foreground.
1) Should the service (Starter) always be declared in the foreground?
2) If not (1) can it be used in the background?

I ask since I use the services to receive audio/video streaming.

If I use services in the foreground, do I have to establish a notification, which in turn do I have to request permission for said notification? Is that so? or am I wrong?
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I am sure the Starter service is my only service.

Will add the code Erel suggested and report back (may take a week or so. seems like only 1 or 2 users are crashing with this - and most of my users are once a week users)
 
Upvote 0

skaliwag

Member
Licensed User
Longtime User
Is everbody else experiencing this problem also displaying Ads in their app?
Just investigating possibilities.
 
Last edited:
Upvote 0

Enxix

Member
Hello, I am sharing my experience with the service (Starter).
I made an application using the service.
I established the permissions in the manifest and their respective type.
The Starter didn't give me a problem, I started it by setting the foreground never.
and I changed it to the foreground when I needed it and if the user gave me notification permission.
Following those steps they accepted my application.
 
Upvote 0

skaliwag

Member
Licensed User
Longtime User
There are no other services in the project.
I am not calling StartService anywhere.
I have added the suggested modification to Service_Create, and will report back when I have more Analytics data.
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
It has been 5 days since I implemented Erel's
B4X:
Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER
and haven't seen a crash on it yet.

But 5 days isn't enough to to cycle thru my users weekly using. But looking good
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Well I got two crashes today - for some reason Google isn't showing ANY stack trace

B4X:
Crash details
android.app.ActivityThread.throwRemoteServiceException

    android.app.ForegroundServiceDidNotStartInTimeException
B4X:
ANR details
anywheresoftware.b4a.BA$B4AExceptionHandler.uncaughtException

    Context.startForegroundService() did not then call Service.startForeground()

Not sure what to make of either of them - for some reason there wasn't any stack trace.

B4X:
Sub Service_Create
    'This is the program entry point.
    'This is a good place to load resources that are not specific to a single activity.
   
    LogColor("Service_Create", Colors.Magenta)
   
    '-------------------------------------------------------------------------------------
    '    Erel's fix to handle Service crash
    '-------------------------------------------------------------------------------------
    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER  
   
End Sub

Again this seems to be only happening on 1 device


 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Just a follow up. Still crashing.

When I had this line of code that Erel recommended
B4X:
Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER

Still was getting the crashes & ANRs but no report data.

So I removed the line and now at least I am back to getting a crash report and some how to fix from Googles Gemini.

But I do not start the "Start" the "Starter" service. So I do not know/understand how I can add the pr-checks that Google Gemini is asking me to do


WHAT I find as very strange is that I have not heard from the user that the system is crashing. Normally if anything goes wrong my users report it.
Could it be that this crash is really not effecting anything and the App simply rolls on?



B4X:
Summary

The app is crashing because it's trying to start a foreground service without providing a foreground service type. This is happening in the handleStartIntent method of the ServiceHelper class from the anywheresoftware.b4a.objects package, which is likely a library used for building the app. The crash occurs when the StartForeground method is called without the necessary foreground service type. This is a new requirement in recent Android versions, and the app needs to be updated to handle it.
Potential Solutions

    Specify a Foreground Service Type:

    The most likely cause of the crash is that the StartForeground method is being called without a foreground service type. To fix this, you need to provide a valid foreground service type when calling this method.

    For example, if your service is playing audio in the background, you would use the FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK type:

    // Assuming 'context' is available within the 'handleStartIntent' method
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        ServiceHelper.StartForeground(context, 1, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK);
    } else {
        ServiceHelper.StartForeground(context, 1, notification);
    }

    Make sure to choose the appropriate foreground service type based on the functionality of your service. You can find a list of available types in the Android documentation for ServiceInfo.

    Check for Older Android Versions:

    It's possible that the ServiceHelper library you are using doesn't handle the foreground service type requirement correctly for older Android versions. You can add a version check before calling StartForeground to ensure that the type is only provided on Android versions that require it (Android 10 and above).

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        // Call StartForeground with the appropriate type
    } else {
        // Call StartForeground without the type
    }

    This approach ensures backward compatibility while addressing the crash on newer Android versions.

B4X:
Exception java.lang.RuntimeException:
  at android.app.ActivityThread.handleServiceArgs (ActivityThread.java:5286)
  at android.app.ActivityThread.-$$Nest$mhandleServiceArgs
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2531)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loopOnce (Looper.java:230)
  at android.os.Looper.loop (Looper.java:319)
  at android.app.ActivityThread.main (ActivityThread.java:8934)
  at java.lang.reflect.Method.invoke
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:578)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1103)
Caused by java.lang.RuntimeException:
  at anywheresoftware.b4a.objects.ServiceHelper$StarterHelper.handleStartIntent (ServiceHelper.java:173)
  at com.BOBs.BBS.starter.handleStart (starter.java:102)
  at com.BOBs.BBS.starter.access$000 (starter.java:8)
  at com.BOBs.BBS.starter$1.run (starter.java:74)
  at anywheresoftware.b4a.objects.ServiceHelper$StarterHelper.onStartCommand (ServiceHelper.java:240)
  at com.BOBs.BBS.starter.onStartCommand (starter.java:72)
  at android.app.ActivityThread.handleServiceArgs (ActivityThread.java:5268)
Caused by android.app.MissingForegroundServiceTypeException:
  at android.app.MissingForegroundServiceTypeException$1.createFromParcel (MissingForegroundServiceTypeException.java:53)
  at android.app.MissingForegroundServiceTypeException$1.createFromParcel (MissingForegroundServiceTypeException.java:49)
  at android.os.Parcel.readParcelableInternal (Parcel.java:4882)
  at android.os.Parcel.readParcelable (Parcel.java:4864)
  at android.os.Parcel.createExceptionOrNull (Parcel.java:3064)
  at android.os.Parcel.createException (Parcel.java:3053)
  at android.os.Parcel.readException (Parcel.java:3036)
  at android.os.Parcel.readException (Parcel.java:2978)
  at android.app.IActivityManager$Stub$Proxy.setServiceForeground (IActivityManager.java:7234)
  at android.app.Service.startForeground (Service.java:775)
  at anywheresoftware.b4a.objects.ServiceHelper.StartForeground (ServiceHelper.java:85)
  at anywheresoftware.b4a.objects.ServiceHelper$StarterHelper.handleStartIntent (ServiceHelper.java:171)
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Erel would need to to comment on this but looking at line 173 in anywheresoftware.b4a.objects.ServiceHelper$StarterHelper.handleStartIntent it is re-throwing an Exception that was caught when it invoked sh.StartForeground(sh.autoNotificationId, sh.AutomaticForegroundNotification); Tracing back this in turn calls service.startForeground(Id, notification); in ServiceHelper so it looks to me, unless I am missing something, that B4A is starting a foreground service with the older version of StartForeground with two parameters instead of the later one with three parameters specifying the service type required by Android 14 as shown in the summary above. So it looks to me that the MissingForegroundServiceTypeException is valid. I have no idea what code paths bring it to here and under what circumstances - hence we need Erel!
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
many thanks to @agraham for raising an interesting point.
in spite of its name, StartForeground() doesn't start a foreground service. StartForegroundService() does.
StartForeground() moves a running service from background to foreground to post its notification.
StartForeground() is currently overloaded to allow for the passing of the so-called foregroundservicetype
variable (now required as of android 14). if the type is not passed, the system will refer to the
foregroundservicetype declared in the manifest. if none has been declared there, an exception wil be thrown.
otherwise, no exception. android's documentation regarding foreground services does its best to obfuscate
this, but if you stare at it long enough the veil is lifted.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…