如何一次性授予所有Android权限?

时间:2017-02-28 08:34:32

标签: android adb android-permissions android-espresso

在我的espresso咖啡测试的@Before中我正在使用

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

我是否可以通过单一命令授予多个权限(或所有权限)?

同样撤销permsission会导致应用程序崩溃并导致测试失败。是否有解决方法?

4 个答案:

答案 0 :(得分:1)

试试这个,

    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
          }

获取权限结果

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

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

答案 1 :(得分:1)

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

你可以像https://inthecheesefactory.com/blog/things-you-need-to-know-about-android-m-permission-developer-edition/en

那样实现它

答案 2 :(得分:1)

试试这个。它会帮助你。

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

答案 3 :(得分:1)

您可以使用;

授予安装的所有运行时权限

adb install -g

或者只是在每个pm赠款之间使用&#39 ;;'

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