Android Question SOLVED: B4A 13 targeting SDK 34 ... how to start service?

Greg Breinholt

Member
Licensed User
Longtime User
When targeting SDK 34 there are new requirements for starting a service, where you have to include a foreground service type...
https://developer.android.com/about/versions/14/changes/fgs-types-required

There is this tutorial page, but it does not mention using the Service.StartForground:
https://www.b4x.com/android/forum/threads/android-14-targetsdkversion-34-and-services.162140/

I have updated the manifest with the required types, but with my current code (targeting SDK 34), the last line causes an issue as no type is specified:

B4X:
' Creates a notification
    Dim Notif As Notification
    Notif.Initialize2(Notif.IMPORTANCE_LOW)
    Notif.Icon = "ic_notification"
    Notif.OnGoingEvent = True
    Notif.Sound = False
    Notif.Vibrate = False
    Notif.Light = False
    Notif.SetInfo(ResourceStrings.Get("FloatingVolumeControl"), ResourceStrings.Get("ServiceNotificationText"), Main)
    'Brings the current Service To the foreground state And displays the notification
    Service.StartForeground(1, Notif)


Error:
B4X:
android.app.MissingForegroundServiceTypeException: Starting FGS without a type  callerApp=ProcessRecord{610824 10074:com.iprototypes.volume/u0a190} targetSDK=34


If it was calling from JAVA, you can send the extra parameter (the foreground service type):
Java:
 if (Build.VERSION.SDK_INT >= 34) {
        service.startForeground(
                NOTIFICATION_ID,
                notification,
                ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE);
        }else {
            service.startForeground(
                    NOTIFICATION_ID,
                    notification);
 }



Does anyone know another way to achieve this?
Or will this need an update to B4A to handle starting a service with the extra parameter?

Solved = see answers below.
 
Last edited:
Solution
No need for the additional parameter. You do need to declare the foreground type in the manifest editor.
Yes, solved, thank you!

Updated the manifest to include a name and value to the service property (to explain the apps use of the "specialUse" type), plus adding the this line solved it:

B4X:
SetServiceAttribute(svcoverlayinfo, android:foregroundServiceType, "specialUse")

Greg Breinholt

Member
Licensed User
Longtime User
No need for the additional parameter. You do need to declare the foreground type in the manifest editor.
Yes, solved, thank you!

Updated the manifest to include a name and value to the service property (to explain the apps use of the "specialUse" type), plus adding the this line solved it:

B4X:
SetServiceAttribute(svcoverlayinfo, android:foregroundServiceType, "specialUse")
 
Upvote 0
Solution
Top