foregroundServiceType
attribute in manifest
foregroundServiceType
attribute in manifestThis is an error.
| |
Missing | |
Error | |
Correctness | |
Android | |
Android Open Source Project | |
8.2.0 (November 2023) | |
Kotlin and Java files and manifest files | |
This check runs on the fly in the IDE editor | |
targetSdkVersion
>= 34, to call Service.startForeground()
, the
foregroundServiceType
attribute specified.
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.
You can suppress false positives using one of the following mechanisms:
tools:ignore="ForegroundServiceType"
on the problematic XML
element (or one of its enclosing elements). You may also need to add
the following namespace declaration on the root element in the XML
file if it's not already there:
xmlns:tools="http://schemas.android.com/tools"
.
// Kotlin
@Suppress("ForegroundServiceType")
fun method() {
startForeground(...)
}
or
// Java
@SuppressWarnings("ForegroundServiceType")
void method() {
startForeground(...);
}
//noinspection ForegroundServiceType
problematicStatement()
lint.xml
file in the source tree which turns off
the check in that folder and any sub folder. A simple file might look
like this:
<?xml version="1.0" encoding="UTF-8"?>
<lint>
<issue id="ForegroundServiceType" severity="ignore" />
</lint>
Instead of ignore
you can also change the severity here, for
example from error
to warning
. You can find additional
documentation on how to filter issues by path, regular expression and
so on
here.
lintOptions {
disable 'ForegroundServiceType'
}
In Android projects this should be nested inside an android { }
block.
lint
, using the --ignore
flag:
$ lint --ignore ForegroundServiceType ...`