I’m using foreground service special with updates for Android 14 as per Google guidelines.
On most Android 14 devices this works fine, but on certain devices I get the following exception
which I’m catching:
Non-fatal Exception: java.lang.SecurityException
Starting FGS with type specialUse callerApp=ProcessRecord{a5fde95 20787:com.myapp/u0a432} targetSDK=34 requires permissions: all of the permissions allOf=true [android.permission.FOREGROUND_SERVICE_SPECIAL_USE]
This is triggered by the following code:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
{
ServiceCompat.startForeground(this, COUNTDOWN_SERVICE_ID, notification, FOREGROUND_SERVICE_TYPE_SPECIAL_USE);
}
else
{
startForeground(COUNTDOWN_SERVICE_ID, notification);
}
I’ve seen issues with other bits of code whereby I have to explicitly check for the service permission at runtime on Samsung devices even though it’s granted, so I added the following:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.FOREGROUND_SERVICE) == PackageManager.PERMISSION_GRANTED)
{
// start foreground service
but that hasn’t fixed it. If I check for if (ContextCompat.checkSelfPermission(this, Manifest.permission.FOREGROUND_SERVICE_SPECIAL_USE) == PackageManager.PERMISSION_GRANTED)
then it returns false on the Android 14 devices I’ve tested.
For completeness my Manifest has the following:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE"/>
...
<service
android:name=".CountService"
android:foregroundServiceType="specialUse"
android:exported="false">
<property
android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
android:value="[Description of why foreground service is needed]" />
</service>
Google explicitly says there are no runtime checks needed for FGS specialUse. Anyone know what’s missing?