Having followed the migration notes and added the following line to the manifest :

<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

I could not figure out why I am getting a failing test case where the permission android.permission.FOREGROUND_SERVICE is not being granted (at least when it's being checked on run-time).

Based on the docs, which states that android.permission.FOREGROUND_SERVICEshould be automatically granted by the system.

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.

Can somebody explain why test case (which uses a variant of the the following code) is failing?

assert (ActivityCompat.checkSelfPermission(activity, "android.permission.FOREGROUND_SERVICE") == PackageManager.PERMISSION_GRANTED)

Yet! I am not having any SecurityException thrown. Which makes me doubt about the way I should check permissions on run-time.

What am I missing?

2 Answers

Derek K On Best Solutions

This is failing because you probably run it on device with API < 28. This method field works only on API 28 and up. If you write as you should Manifest.permission.FOREGROUND_SERVICE instead of plain string, Android Studio will warn you.

CommonsWare On

FOREGROUND_SERVICE is not a dangerous permission. Hence, it has nothing to do with the runtime permission system and methods like checkSelfPermission().