Missing foregroundServiceType attribute in manifest

Missing foregroundServiceType attribute in manifest

This is an error.

Id

ForegroundServiceType

Summary

Missing foregroundServiceType attribute in manifest

Severity

Error

Category

Correctness

Platform

Android

Vendor

Android Open Source Project

Feedback

https://issuetracker.google.com/issues/new?component=192708

Since

8.2.0 (November 2023)

Affects

Kotlin and Java files and manifest files

Editing

This check runs on the fly in the IDE editor

Implementation

Source Code

Tests

Source Code

For targetSdkVersion >= 34, to call Service.startForeground(), the element in the manifest file must have the foregroundServiceType attribute specified.

Example

Here is an example of lint warnings produced by this check:

src/test/pkg/MyService.java:8:Error: To call Service.startForeground(),
the <service> element of manifest file must have the
foregroundServiceType attribute specified [ForegroundServiceType]
    startForeground(1, null);
    ---------------

Here are the relevant source files:

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="test.pkg"> <uses-sdk android:targetSdkVersion="34" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <application> <service android:exported="true" android:name="test.pkg.MyService"> </service> </application> </manifest>

src/test/pkg/MyService.java:

package test.pkg; import android.app.Service; import android.content.Intent; import android.os.IBinder; public class MyService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { startForeground(1, null); return START_NOT_STICKY; } @Override public IBinder onBind(Intent intent) { return null; } }

You can also visit the source code for the unit tests for this check to see additional scenarios.

Suppressing

You can suppress false positives using one of the following mechanisms:

formatted by Markdeep 1.18