Shashi Kumar Raja
Shashi Kumar Raja

Reputation: 518

How to grant all android permissions in one go?

In @Before of my espresso test I am using

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        getInstrumentation().getUiAutomation().executeShellCommand(
                "pm grant " + getTargetContext().getPackageName()
                        + " android.permission.SEND_SMS");

Is there any way I can grant more than one permission(or all permissions) with single command only?

Also revoking the permsission kills the app and causes test failure.Is there any workaround for that?

Upvotes: 2

Views: 13011

Answers (4)

Burak Day
Burak Day

Reputation: 959

You can grant all run-time permissions on installation by using;

adb install -g.

Or just use ';' between each pm grants like this?

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    getInstrumentation().getUiAutomation().executeShellCommand(
            "pm grant " + getTargetContext().getPackageName()
                    + " android.permission.SEND_SMS;" 

            + "pm grant " + getTargetContext().getPackageName()
                    + " android.permission.WRITE_EXTERNAL_STORAGE");
}

Upvotes: 2

Joy Helina
Joy Helina

Reputation: 378

Try this. it will help you.

public 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 (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
                    return false;
                }
            }
        }
        return true;
    }


    public void givePermisson(){
        int PERMISSION_ALL = 1; 
        String[] PERMISSIONS = {Manifest.permission.READ_CONTACTS, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.READ_PHONE_STATE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_SMS, Manifest.permission.CAMERA,Manifest.permission.RECEIVE_SMS};

        if(!hasPermissions(this, PERMISSIONS)){
            requestPermissions(PERMISSIONS, PERMISSION_ALL);
        }
    }

Upvotes: 1

Shaz Hemani
Shaz Hemani

Reputation: 469

requestPermissions(new String[]{
                            Manifest.permission.READ_CONTACTS,
                            Manifest.permission.ACCESS_FINE_LOCATION},
                    ASK_MULTIPLE_PERMISSION_REQUEST_CODE);

You can acheive it like this https://inthecheesefactory.com/blog/things-you-need-to-know-about-android-m-permission-developer-edition/en

Upvotes: 1

Komal12
Komal12

Reputation: 3348

Try this,

    private static final int REQUEST_STORAGE = 112;


      if (Build.VERSION.SDK_INT >= 23) {
            String[] PERMISSIONS = {android.Manifest.permission.WRITE_EXTERNAL_STORAGE, android.Manifest.permission.READ_EXTERNAL_STORAGE};//Add all permission here
            if (!hasPermissions(mContext, PERMISSIONS)) {
                ActivityCompat.requestPermissions((Activity) mContext, PERMISSIONS, REQUEST_STORAGE );
            } else {
               //TO Do
            }
          } else {
              //TO Do
          }

get Permissions Result

        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            switch (requestCode) {
                case REQUEST_STORAGE: {
                    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                      //To Do
                    } else {
                        Toast.makeText(mContext, "The app was not allowed.", Toast.LENGTH_LONG).show();
                    }
                }
            }
        }

check permissions for marshmallow

    private static boolean hasPermissions(Context context, String... permissions) {
        if (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;
    }

Upvotes: 1

Related Questions