javaandroidandroid-permissionsforeground-service

checking if FOREGROUND_SERVICE permission is granted allways returns false


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 granted (at least when it's being checked at 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 my test case (which uses a variant of the following code) is failing?

assertEquals(ActivityCompat.checkSelfPermission(activity, "android.permission.FOREGROUND_SERVICE"), PackageManager.PERMISSION_GRANTED);

Yet! I do not have any SecurityException thrown. What am I missing?


Solution

  • 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.