I have implemented Foreground Service using WorkManager, it works fine upto API Level 33. Since it is mandatory in API Level 34 to specify foregroundServiceType, I specified it in Worker and in the Manifest but getting this error.
AndroidManifest.xml
Required Permissions:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<service
android:name=".services.DownloadService"
android:exported="false"
android:enabled="true"
tools:node="merge"
android:foregroundServiceType="dataSync"/>
Inside Worker:
setForegroundAsync(
ForegroundInfo(
NOTIFICATION_ID,
getNotification(),
ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC
)
)
Getting this error
java.lang.IllegalArgumentException: foregroundServiceType 0x00000001 is not a subset of foregroundServiceType attribute 0x00000000 in service element of manifest file at android.os.Parcel.createExceptionOrNull(Parcel.java:3015)
at android.os.Parcel.createException(Parcel.java:2995)
at android.os.Parcel.readException(Parcel.java:2978)
at android.os.Parcel.readException(Parcel.java:2920)
at android.app.IActivityManager$Stub$Proxy.setServiceForeground(IActivityManager.java:6079) at android.app.Service.startForeground(Service.java:797) ...
I tried making some changes here and there but nothing works.
Thanks to @sebastian I got the right answer for this exception, when no services are used in application, but WorkManager is used.
The full answer would be to add into AndroidManifest.xml
<service
android:name="androidx.work.impl.foreground.SystemForegroundService"
android:foregroundServiceType="dataSync"
tools:node="merge" />
Also add proper permission in the same file, based on foregroundServiceType
that is defined for that service. In this case, add permission:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC"/>
And to call startForegroundAsync
, create ForegroundInfo on the following way:
ForegroundInfo foregroundInfo;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
foregroundInfo = new ForegroundInfo(notificationId, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC);
} else {
foregroundInfo = new ForegroundInfo(notificationId, notification);
}
setForegroundAsync(foregroundInfo);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With