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
  • 5,260
  • 6
  • 34
  • 51
Jame
  • 3,746
  • 6
  • 52
  • 101
  • 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

0 Answers0