[Fixed] Why am I encountering the error 'Starting FGS without a type' when executing the code on Android 14? – Android

by
Alexei Petrov
android kotlin

Quick Fix: From Android 14, set a foreground service type. For media apps, add FOREGROUND_TYPE_MEDIA_PLAYBACK to startForground and add relevant permission and <uses-service-type>xxx</uses-service-type>. For other types, change xxx to the appropriate type.

The Problem:

In Android 14, when attempting to initiate a foreground service named ‘startForegroundService()’, an error called ‘android.app.MissingForegroundServiceTypeException’ is encountered. This issue only occurs on Android 14 and above, while the code operates as expected on older Android versions. The error message specifically mentions ‘Starting FGS without a type’. The goal is to resolve this error by modifying the code so that it functions correctly on Android 14 and maintains compatibility with earlier versions.

The Solutions:

Solution 1: Add foreground service type

To fix the error “Starting FGS without a type” in Android 14, you need to set the foreground service type when starting the service. This is because Android 14 (SDK 34) requires you to specify the type of foreground service you are starting.

To set the foreground service type, you can use the startForeground() method with the FOREGROUND_SERVICE_TYPE_ constant. For example:

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
    startForeground(SERVICE_ID, notification);
} else {
    startForeground(SERVICE_ID, notification, FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK);
}

You also need to add the FOREGROUND_SERVICE_ permission to your manifest file. This permission is required for apps targeting Android 14 and higher that want to use foreground services.

<uses-permission
    android:name="android.permission.FOREGROUND_SERVICE_xxx"
    android:minSdkVersion="34" />

Finally, you need to specify the foreground service type in the manifest file. You can do this by adding the foregroundServiceType attribute to the service element.

<application ...>
    <service
        android:name=".feature.exerciseplayer.data.service.YourService"
        android:exported="true"
        android:foregroundServiceType="xxx" />
</application>

Note: Replace xxx with the service type that suits your app. For example, if your app is a media player, you can use FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK.

Q&A

What is the reason of getting "Starting FGS without a type" error?

You should set foreground service type from Android 14 onwards.

How can I fix this error?

Set foreground service type only when compiling with minSdkVersion 34 and above.

What should be added to manifest file?

Add necessary permissions in manifest file with android:minSdkVersion="34"

Video Explanation:

The following video, titled "Related Video", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

ZBrush - Fix the ERROR: Zremeshing error encountered. There are a couple of different ways to fix this, see them here.