简体   繁体   中英

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?

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;
    }
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

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);
        }
    }

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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM