3

I am making an application which allows to automatically add the permission for Android 6.0 and Android M. My permission include

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

I used below code to add permission for Android 6.0 version

public static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if(checkAndRequestPermissions()) {
        Log.d ("TAG", "Permissions are granted");
    }
}

private  boolean checkAndRequestPermissions() {
    if (Build.VERSION.SDK_INT >= 23) {
        int permissionSTORAGE  = ContextCompat.checkSelfPermission(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE);
        int permissionRECORDAUDIO  = ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO);
        List<String> listPermissionsNeeded = new ArrayList<>();
        if (permissionSTORAGE != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }
        if (permissionRECORDAUDIO != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.RECORD_AUDIO);
        }
        if (!listPermissionsNeeded.isEmpty()) {
            ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS);
            return false;
        }
        return true;
    }
    else
        return true;
}}

I have two issues in above code.

  1. Is it possible to check permission in Emulator (Android M, SDK 24)?
  2. Is my implementation correct? I don't have real device, so I cannot confirm above code.
Burhanuddin Rashid
  • 4,992
  • 5
  • 34
  • 51
Jame
  • 3,584
  • 5
  • 49
  • 92
  • its confirmed work here have look http://stackoverflow.com/questions/38141523/directory-creation-not-working-in-marshmallow-android/38141778#38141778 – Sohail Zahid Aug 22 '16 at 13:15
  • check this guide -- https://inthecheesefactory.com/blog/things-you-need-to-know-about-android-m-permission-developer-edition/en -- Not Sure about the emulator i but i have tested on on real device. Should work the same – Tasos Aug 22 '16 at 13:21

2 Answers2

2

please read my code correctly and modify according your need

just include all 4 permissions in the ActivityCompat.requestPermissions(...) call and Android will automatically page them together like you mentioned.

I have a helper method to check multiple permissions and see if any of them are not granted.

public static boolean hasPermissions(Context context, String... permissions) {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
        for (String permission : permissions) {
            if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
    }
    return true;
}

Then just send it all of the permissions. Android will ask only for the ones it needs.

String[] PERMISSIONS = {Manifest.permission.RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE};

if(!hasPermissions(this, PERMISSIONS)){
    ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
}
Riyaz Parasara
  • 1,296
  • 12
  • 24
1

There's a Sample app provided by Google, please see the Sample here on Github.

Spacifically see the code of MainActivity for how to check for multiple permissions.

Hope it helps!

Shridutt Kothari
  • 7,819
  • 4
  • 43
  • 61