Android6.0 权限授予


参考 :http://blog.csdn.net/lmj623565791/article/details/50709663
https://developer.android.com/training/permissions/requesting.html?hl=zh-cn

Check For Permissions

If your app needs a dangerous permission, you must check whether you have that permission every time you perform an operation that requires that permission. The user is always free to revoke the permission, so even if the app used the camera yesterday, it can't assume it still has that permission today.

To check if you have a permission, call the [ContextCompat.checkSelfPermission()](https://developer.android.com/reference/android/support/v4/content/ContextCompat.html?hl=zh-cn#checkSelfPermission(android.content.Context, java.lang.String))method. For example, this snippet shows how to check if the activity has permission to write to the calendar:

// Assume thisActivity is the current activity
int permissionCheck = 
ContextCompat.checkSelfPermission(thisActivity,  
      Manifest.permission.WRITE_CALENDAR);

If the app has the permission, the method returns PackageManager.PERMISSION_GRANTED
, and the app can proceed with the operation. If the app does not have the permission, the method returns PERMISSION_DENIED,and the app has to explicitly ask the user for permission.

boolean hasPermision = 
ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;

6.0 运行时权限的变化及特点

对于6.0以下的权限及在安装的时候,根据权限声明产生一个权限列表,用户只有在同意之后才能完成app的安装,造成了我们想要使用某个app,就要默默忍受其一些不必要的权限(比如是个app都要访问通讯录、短信等)。而在6.0以后,我们可以直接安装,当app需要我们授予不恰当的权限的时候,我们可以予以拒绝(比如:单机的象棋对战,请求访问任何权限,我都是不同意的)。当然你也可以在设置界面对每个app的权限进行查看,以及对单个权限进行授权或者解除授权。
新的权限机制更好的保护了用户的隐私,Google将权限分为两类,一类是Normal Permissions,这类权限一般不涉及用户隐私,是不需要用户进行授权的,比如手机震动、访问网络等;另一类是Dangerous Permission,一般是涉及到用户隐私的,需要用户进行授权,比如读取sdcard、访问通讯录等。
可以通过

adb shell pm list permissions -d -g

进行查看。

Github : https://github.com/hongyangAndroid/MPermissions

  toTo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(ContextCompat.checkSelfPermission(MainActivity2.this,
                        Manifest.permission.READ_SMS)
                        != PackageManager.PERMISSION_GRANTED){
                    if (MPermissions.shouldShowRequestPermissionRationale(MainActivity2.this, Manifest.permission.READ_SMS, REQUECT_CODE_SDCARD)){
                    }else{
                        MPermissions.requestPermissions(MainActivity2.this, REQUECT_CODE_SDCARD, Manifest.permission.READ_SMS);
                    }
                }else{
                    Toast.makeText(MainActivity2.this, "已经有权限", Toast.LENGTH_SHORT).show();
                }
            }
        })

弹出提示框 通过注解 @ShowRequestPermissionRationale


 @ShowRequestPermissionRationale(REQUECT_CODE_SDCARD)
    public void showdialog(){
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity2.this);
        builder.setTitle("说明")
                .setMessage("需要使用电话权限,进行电话测试")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        MPermissions.requestPermissions(MainActivity2.this, REQUECT_CODE_SDCARD, Manifest.permission.READ_SMS);
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        return;
                    }
                })
                .create()
                .show();
    }

Android M 即6.0 需要权限时 APP 系统会申请相应的权限

在Manifast 中需要写入权限,如果没有在Manifast 中写入权限,进行权限授予时会直接显示拒绝授权;APP运行需要权限时要检查是否被授予权限

Android6.0 权限授予_第1张图片
77BE.tmp.png

[ContextCompat.checkSelfPermission()](http://developer.android.com/reference/android/support/v4/content/ContextCompat.html#checkSelfPermission(android.content.Context, java.lang.String)) 先检查是否授权 ,设置为false,默认是未授权的,然后进行权限申请,
如果拒绝,第二次进行权限申请时会弹出为什么要用此权限

Android6.0 权限授予_第2张图片
6DD.tmp.png

点击确认后,再次弹出

Android6.0 权限授予_第3张图片
CE94.tmp.png

你可能感兴趣的:(Android6.0 权限授予)