Android6.0 权限授予

作者: 乘风破浪的程序员 | 来源:发表于2016-07-22 17:01 被阅读235次

参考 :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运行需要权限时要检查是否被授予权限

77BE.tmp.png

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

6DD.tmp.png

点击确认后,再次弹出

CE94.tmp.png

相关文章

  • Android6.0 权限授予

    参考 :http://blog.csdn.net/lmj623565791/article/details/507...

  • Android6.0运行时权限处理

    概述 从Android6.0(API23)开始,用户可以在应用运行时向其授予权限,而不是在应用安装时授予。在And...

  • Android运行时权限

    注意 Android6.0系统默认为targetSdkVersion小于23的应用默认授予了所申请的所有权限,所以...

  • 运行时权限

    Android6.0中新添加了运行时权限,对于一些敏感的权限系统要求等用户确认授予权限后才可使用。如果没做Andr...

  • Android 运行时权限

    Android6.0以后Google不再允许开发者直接或许应用的权限,需要在用户知情的情况下授予权限,即需要用到权...

  • Android6.0 动态权限 笔录

    导航: Android6.0 动态权限申请程序 普通权限与动态权限 完整代码 在Android6.0中,系统权限被...

  • 6.0运行时权限与targetSdkVersion选择

    Android6.0普通权限normal permission 和 危险权限dangerous permissio...

  • 关于android6.0的权限申请

    谷歌在android6.0的权限管理方面做了不少的改动,android6.0以前的话需要什么权限只需要在Andro...

  • Android 6.0 RuntimePermission

    android6.0棉花糖,app将不会在安装的时候授予权限。取而代之的是,app不得不在运行时一个一个询问用户授...

  • Android权限检查API checkSelfPermissi

    Android6.0之后,权限分为install时的权限跟运行时权限,如果我们的targetSdkVersion>...

网友评论

    本文标题:Android6.0 权限授予

    本文链接:https://www.haomeiwen.com/subject/gnrijttx.html