0

I am working on giving the runtime permissions to my android device . In my case as soon as user clicks on Call button it should ask for 3 permissions CALL_PHONE,CAMERA and RECORD_AUDIO. It is asking for permissions I can see in the log but only one of the permission is being granted. Again if I kill my app and click on Call button now second permission is being asked and nxt time the lats one. All 3 are not being asked at the same time. As I can see in the log it asks and grants all the permission in the first time only but only one is asked in one time.

 @JavascriptInterface
    public void askPermission(String permission){
        Log.d("Ask permission", permission);
        switch (permission){
        case "audioCall":
            askForPermission(Manifest.permission.CALL_PHONE,CALL_PHONE);
            askForPermission(Manifest.permission.CAMERA,CAMERA);
            askForPermission(Manifest.permission.RECORD_AUDIO,RECORD_AUDIO);
            break;
        case "videoCall":
            askForPermission(Manifest.permission.CALL_PHONE,CALL_PHONE);
            askForPermission(Manifest.permission.CAMERA,CAMERA);
            askForPermission(Manifest.permission.RECORD_AUDIO,RECORD_AUDIO);
            break;

        case "writeExternalStorage":
            askForPermission(Manifest.permission.CALL_PHONE,CALL_PHONE);
            askForPermission(Manifest.permission.CAMERA,CAMERA);
            askForPermission(Manifest.permission.RECORD_AUDIO,RECORD_AUDIO);
            break;

        case "WRITE_EXTERNAL_STORAGE":
            askForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE,WRITE_EXTERNAL_STORAGE);
            break;

        case "GET_ACCOUNTS":
            askForPermission(Manifest.permission.GET_ACCOUNTS,GET_ACCOUNTS);
            break;

        case "READ_CONTACTS":
            askForPermission(Manifest.permission.READ_CONTACTS,READ_CONTACTS);
            break;

        }
    }

public void askForPermission(String permission, Integer requestCode){
    Log.d("Permissions requested is+", permission);

    if (ContextCompat.checkSelfPermission(MainActivity.this, permission) != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, permission)) {
            Log.d("Permission is already granted", ""+permission);
            //This is called if user has denied the permission before
            //In this case I am just asking the permission again
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{permission}, requestCode);

        } else {
            Log.d("asking for permissions", ""+permission);
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{permission}, requestCode);
        }
    } else {
        /* Toast.makeText(this, "" + permission + " is already granted.", Toast.LENGTH_SHORT).show();*/
        Log.d("Permission is already granted", ""+permission);
    }
}

logs.txt

12-01 15:36:54.911: D/Ask permission(20783): videoCall
12-01 15:36:54.912: D/Permissions requested is+(20783): android.permission.CALL_PHONE
12-01 15:36:54.912: D/Permission is already granted(20783): android.permission.CALL_PHONE
12-01 15:36:54.912: D/Permissions requested is+(20783): android.permission.CAMERA
12-01 15:36:54.913: D/Permission is already granted(20783): android.permission.CAMERA
12-01 15:36:54.921: D/Permissions requested is+(20783): android.permission.RECORD_AUDIO
12-01 15:36:54.923: D/Permission is already granted(20783): android.permission.RECORD_AUDIO
Kalpit
  • 47
  • 7
  • Why dont you try asking all permissions at one ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.GET_ACCOUNTS, Manifest.permission.READ_SMS, Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_GET_ACCOUNTS); – Tasneem Dec 01 '16 at 10:22
  • `working on giving the runtime permissions to my android device`. No not to your device. But to your app. Code so the user can grant permissions to your app. – greenapps Dec 01 '16 at 10:25
  • Check [this](http://stackoverflow.com/a/40798994/6422096) link it might help you! – Satish Silveri Dec 01 '16 at 11:44
  • @greenapps I mean the same. Permission to my app. – Kalpit Dec 05 '16 at 05:26
  • @Tasneem Asking all permissions at the same time is not good practice . Sometimes even your app gets rejected for the same reason – Kalpit Dec 05 '16 at 05:27

0 Answers0