This will happen if you have set targetSdkVersion = 28
(Android 9 / Pie) or above and have not declared the usage of the FOREGROUND_SERVICE
permission.
From the migration notes for Android 9:
Apps wanting to use foreground services must now request the FOREGROUND_SERVICE permission first. This is a normal permission, so the system automatically grants it to the requesting app. Starting a foreground service without the permission throws a SecurityException.
The solution is to just add the following in AndroidManifest.xml
:
<manifest ...>
...
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
...
<application ...>
...
</manifest>
Permission Denial: startForeground requires android.permission.FOREGROUND_SERVICE
Apps that target Android 9 (API level 28) or higher and use foreground services must request the FOREGROUND_SERVICE permission
.
So now we need to add Foreground service permission in manifest file
Service.startForeground
SAMPLE
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
FOREGROUND_SERVICE
is a normal permission, so the system automatically grants it to the requesting app.
Check this the migration notes of Android 9 / Pie
Foreground service permission
Apps wanting to use foreground services must now request the FOREGROUND_SERVICE permission first. This is a normal permission, so the system automatically grants it to the requesting app. Starting a foreground service without the permission throws a SecurityException.
Also Read startForeground()
Build.VERSION_CODES.P
or later must request the permission Manifest.permission.FOREGROUND_SERVICE
in order to use this API.Noting that FOREGROUND_SERVICE
doesn't require runtime permission requirement. only add below to Manifests
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
the above line should be added before <application
For API level 28 or higher, it requires FOREGROUND_SERVICE permission. Otherwise, it can not run and got an exception.
It will be solved by adding
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
in AndroidManifest.xml file.
A targetSdkVersion
of at least 28
will be mandatory from Nov 1 2019 for app updates in the Play Store. So you'll need to change the target API, and then request the permission FOREGROUND_SERVICE
to avoid the crash on startForeground()
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