0

I am currently develop an android application that api levels between 21-29.My problem is some permissions are not supported below api level 29.For example <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> permission no need to declare below api level 29. When I define this permission and fetch all permissions then ask user to grant permission,application below api level 29 is crashing. I need to define different permissions for different api levels or how can i deal with this problem ?

Here is my manifest.xml

  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.RECORD_AUDIO" />
  <uses-permission android:name="android.permission.CAMERA" />
  <uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
  <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

and here is my code to get all permission

PackageInfo info =
       getPackageManager()
           .getPackageInfo(
               getApplicationContext().getPackageName(), PackageManager.GET_PERMISSIONS);

   ActivityCompat.requestPermissions(this, info.requestedPermissions, PERMISSION_CODE); 



@Override
   public void onRequestPermissionsResult(
       int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
     if (requestCode == PERMISSION_CODE) {

       OptionalInt minValuePermission = Arrays.stream(grantResults).min();

       if (minValuePermission.isPresent()
           && minValuePermission.getAsInt() == PackageManager.PERMISSION_GRANTED) {/**/}

  • The problem appears to be related to your runtime requests rather than the `uses-permission` declarations. So you could create a bunch of string array resource files that you place under `res/values-vNN` and use the contents of those to decide which permissions to request at runtime. – Michael Aug 26 '20 at 09:25

1 Answers1

0

you don't need to declare different lists for different OS versions - permission introduced later will be ommited/ignored on lower platforms

also: not all permissions can be acquired by ActivityCompat.requestPermissions, FOREGROUND_SERVICE isn't for shure (should be granted automatically) and you shouldn't ask for it. so you shouldn't obtain whole list of all declared permissions through PackageManager, instead make own Java/Kotlin array with runtime perms

ActivityCompat.requestPermissions(this,
                // below your array with runtime permissions
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                PERMISSION_CODE);
snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • Thanks you for your reply. This solved my problem. But I don't understand why i shouldn't obtain whole list of all declared permissions? Can you explain? – tametah Aug 26 '20 at 10:32
  • you can, but you have to check which perm you already have and which isn't "runtime" accessible. ask only for runtime ones, some list can be found [HERE](https://inthecheesefactory.com/blog/things-you-need-to-know-about-android-m-permission-developer-edition/en) (old post before `FOREGROUND_SERVICE` introduction, so you won't find it there) – snachmsm Aug 26 '20 at 10:52
  • also found in one of answers in [HERE](https://stackoverflow.com/questions/52382710/permission-denial-startforeground-requires-android-permission-foreground-servic), quotiong: _`FOREGROUND_SERVICE` is a normal permission, so the system automatically grants it to the requesting app._ – snachmsm Aug 26 '20 at 10:54
  • This explanation helped me a lot.Thank you again for your reply – tametah Aug 26 '20 at 11:08