Android 6.0 运行时权限处理完全解析

一、概述

随着Android 6.0发布以及普及,我们开发者所要应对的主要就是新版本SDK带来的一些变化,首先关注的就是权限机制的变化。对于6.0的几个主要的变化,查看查看官网的这篇文章http://developer.android.com/intl/zh-cn/about/versions/marshmallow/android-6.0-changes.html,其中当然包含Runtime Permissions

ok,本篇文章目的之一就是对运行时权限处理的一个介绍,以及对目前权限相关的库的一些了解。

当然非常推荐阅读官网权限相关文章:

本文也是在上述文章基础上理解、实验以及封装。

二、运行时权限的变化及特点

对于6.0以下的权限及在安装的时候,根据权限声明产生一个权限列表,用户只有在同意之后才能完成app的安装,造成了我们想要使用某个app,就要默默忍受其一些不必要的权限(比如是个app都要访问通讯录、短信等)。而在6.0以后,我们可以直接安装,当app需要我们授予不恰当的权限的时候,我们可以予以拒绝(比如:单机的象棋对战,请求访问任何权限,我都是不同意的)。当然你也可以在设置界面对每个app的权限进行查看,以及对单个权限进行授权或者解除授权。

新的权限机制更好的保护了用户的隐私,Google将权限分为两类,一类是Normal Permissions,这类权限一般不涉及用户隐私,是不需要用户进行授权的,比如手机震动、访问网络等;另一类是Dangerous Permission,一般是涉及到用户隐私的,需要用户进行授权,比如读取sdcard、访问通讯录等。

  • Normal Permissions如下
<code class="hljs autohotkey has-numbering">ACCESS_LOCATION_EXTR<span class="hljs-built_in">A_COMMANDS</span>
ACCESS_NETWORK_STATE
ACCESS_NOTIFICATION_POLICY
ACCESS_WIFI_STATE
BLUETOOTH
BLUETOOTH_ADMIN
BROADCAST_STICKY
CHANGE_NETWORK_STATE
CHANGE_WIFI_MULTICAST_STATE
CHANGE_WIFI_STATE
DISABLE_KEYGUARD
EXPAND_STATUS_BAR
GET_PACKAGE_SIZE
INSTALL_SHORTCUT
INTERNET
KILL_BACKGROUND_PROCESSES
MODIFY_AUDIO_SETTINGS
NFC
READ_SYNC_SETTINGS
READ_SYNC_STATS
RECEIVE_BOOT_COMPLETED
REORDER_TASKS
REQUEST_INSTALL_PACKAGES
SET_ALARM
SET_TIME_ZONE
SET_WALLPAPER
SET_WALLPAPER_HINTS
TRANSMIT_IR
UNINSTALL_SHORTCUT
USE_FINGERPRINT
VIBRATE
WAKE_LOCK
WRITE_SYNC_SETTINGS</code>
  • Dangerous Permissions:
<code class="hljs avrasm has-numbering"><span class="hljs-label">group:</span>android<span class="hljs-preprocessor">.permission</span>-group<span class="hljs-preprocessor">.CONTACTS</span>
  permission:android<span class="hljs-preprocessor">.permission</span><span class="hljs-preprocessor">.WRITE</span>_CONTACTS
  permission:android<span class="hljs-preprocessor">.permission</span><span class="hljs-preprocessor">.GET</span>_ACCOUNTS
  permission:android<span class="hljs-preprocessor">.permission</span><span class="hljs-preprocessor">.READ</span>_CONTACTS

<span class="hljs-label">group:</span>android<span class="hljs-preprocessor">.permission</span>-group<span class="hljs-preprocessor">.PHONE</span>
  permission:android<span class="hljs-preprocessor">.permission</span><span class="hljs-preprocessor">.READ</span>_CALL_LOG
  permission:android<span class="hljs-preprocessor">.permission</span><span class="hljs-preprocessor">.READ</span>_PHONE_STATE
  permission:android<span class="hljs-preprocessor">.permission</span><span class="hljs-preprocessor">.CALL</span>_PHONE
  permission:android<span class="hljs-preprocessor">.permission</span><span class="hljs-preprocessor">.WRITE</span>_CALL_LOG
  permission:android<span class="hljs-preprocessor">.permission</span><span class="hljs-preprocessor">.USE</span>_SIP
  permission:android<span class="hljs-preprocessor">.permission</span><span class="hljs-preprocessor">.PROCESS</span>_OUTGOING_CALLS
  permission:<span class="hljs-keyword">com</span><span class="hljs-preprocessor">.android</span><span class="hljs-preprocessor">.voicemail</span><span class="hljs-preprocessor">.permission</span><span class="hljs-preprocessor">.ADD</span>_VOICEMAIL

<span class="hljs-label">group:</span>android<span class="hljs-preprocessor">.permission</span>-group<span class="hljs-preprocessor">.CALENDAR</span>
  permission:android<span class="hljs-preprocessor">.permission</span><span class="hljs-preprocessor">.READ</span>_CALENDAR
  permission:android<span class="hljs-preprocessor">.permission</span><span class="hljs-preprocessor">.WRITE</span>_CALENDAR

<span class="hljs-label">group:</span>android<span class="hljs-preprocessor">.permission</span>-group<span class="hljs-preprocessor">.CAMERA</span>
  permission:android<span class="hljs-preprocessor">.permission</span><span class="hljs-preprocessor">.CAMERA</span>

<span class="hljs-label">group:</span>android<span class="hljs-preprocessor">.permission</span>-group<span class="hljs-preprocessor">.SENSORS</span>
  permission:android<span class="hljs-preprocessor">.permission</span><span class="hljs-preprocessor">.BODY</span>_SENSORS

<span class="hljs-label">group:</span>android<span class="hljs-preprocessor">.permission</span>-group<span class="hljs-preprocessor">.LOCATION</span>
  permission:android<span class="hljs-preprocessor">.permission</span><span class="hljs-preprocessor">.ACCESS</span>_FINE_LOCATION
  permission:android<span class="hljs-preprocessor">.permission</span><span class="hljs-preprocessor">.ACCESS</span>_COARSE_LOCATION

<span class="hljs-label">group:</span>android<span class="hljs-preprocessor">.permission</span>-group<span class="hljs-preprocessor">.STORAGE</span>
  permission:android<span class="hljs-preprocessor">.permission</span><span class="hljs-preprocessor">.READ</span>_EXTERNAL_STORAGE
  permission:android<span class="hljs-preprocessor">.permission</span><span class="hljs-preprocessor">.WRITE</span>_EXTERNAL_STORAGE

<span class="hljs-label">group:</span>android<span class="hljs-preprocessor">.permission</span>-group<span class="hljs-preprocessor">.MICROPHONE</span>
  permission:android<span class="hljs-preprocessor">.permission</span><span class="hljs-preprocessor">.RECORD</span>_AUDIO

<span class="hljs-label">group:</span>android<span class="hljs-preprocessor">.permission</span>-group<span class="hljs-preprocessor">.SMS</span>
  permission:android<span class="hljs-preprocessor">.permission</span><span class="hljs-preprocessor">.READ</span>_SMS
  permission:android<span class="hljs-preprocessor">.permission</span><span class="hljs-preprocessor">.RECEIVE</span>_WAP_PUSH
  permission:android<span class="hljs-preprocessor">.permission</span><span class="hljs-preprocessor">.RECEIVE</span>_MMS
  permission:android<span class="hljs-preprocessor">.permission</span><span class="hljs-preprocessor">.RECEIVE</span>_SMS
  permission:android<span class="hljs-preprocessor">.permission</span><span class="hljs-preprocessor">.SEND</span>_SMS
  permission:android<span class="hljs-preprocessor">.permission</span><span class="hljs-preprocessor">.READ</span>_CELL_BROADCASTS</code>

可以通过adb shell pm list permissions -d -g进行查看。

看到上面的dangerous permissions,会发现一个问题,好像危险权限都是一组一组的,恩,没错,的确是这样的,

那么有个问题:分组对我们的权限机制有什么影响吗?

的确是有影响的,如果app运行在Android 6.x的机器上,对于授权机制是这样的。如果你申请某个危险的权限,假设你的app早已被用户授权了同一组的某个危险权限,那么系统会立即授权,而不需要用户去点击授权。比如你的app对READ_CONTACTS已经授权了,当你的app申请WRITE_CONTACTS时,系统会直接授权通过。此外,对于申请时弹出的dialog上面的文本说明也是对整个权限组的说明,而不是单个权限(ps:这个dialog是不能进行定制的)。

不过需要注意的是,不要对权限组过多的依赖,尽可能对每个危险权限都进行正常流程的申请,因为在后期的版本中这个权限组可能会产生变化。

三、相关API

好在运行时相关的API也比较简单,所以适配起来并不会非常痛苦。

API的讲解就跟着申请权限步骤一起了:

  1. 在AndroidManifest文件中添加需要的权限。

    这个步骤和我们之前的开发并没有什么变化,试图去申请一个没有声明的权限可能会导致程序崩溃。

  2. 检查权限

    <code class="hljs avrasm has-numbering">if (ContextCompat<span class="hljs-preprocessor">.checkSelfPermission</span>(thisActivity,
                    Manifest<span class="hljs-preprocessor">.permission</span><span class="hljs-preprocessor">.READ</span>_CONTACTS)
            != PackageManager<span class="hljs-preprocessor">.PERMISSION</span>_GRANTED) {
    }else{
        //
    }</code>

    这里涉及到一个API,ContextCompat.checkSelfPermission,主要用于检测某个权限是否已经被授予,方法返回值为PackageManager.PERMISSION_DENIED或者PackageManager.PERMISSION_GRANTED。当返回DENIED就需要进行申请授权了。

  3. 申请授权

    <code class="hljs avrasm has-numbering"> ActivityCompat<span class="hljs-preprocessor">.requestPermissions</span>(thisActivity,
                    new String[]{Manifest<span class="hljs-preprocessor">.permission</span><span class="hljs-preprocessor">.READ</span>_CONTACTS},
                    MY_PERMISSIONS_REQUEST_READ_CONTACTS)<span class="hljs-comment">;</span></code>

    该方法是异步的,第一个参数是Context;第二个参数是需要申请的权限的字符串数组;第三个参数为requestCode,主要用于回调的时候检测。可以从方法名requestPermissions以及第二个参数看出,是支持一次性申请多个权限的,系统会通过对话框逐一询问用户是否授权。

  4. 处理权限申请回调

    <code class="language-java hljs  has-numbering"><span class="hljs-annotation">@Override</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onRequestPermissionsResult</span>(<span class="hljs-keyword">int</span> requestCode,
            String permissions[], <span class="hljs-keyword">int</span>[] grantResults) {
        <span class="hljs-keyword">switch</span> (requestCode) {
            <span class="hljs-keyword">case</span> MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
                <span class="hljs-comment">// If request is cancelled, the result arrays are empty.</span>
                <span class="hljs-keyword">if</span> (grantResults.length > <span class="hljs-number">0</span>
                    && grantResults[<span class="hljs-number">0</span>] == PackageManager.PERMISSION_GRANTED) {
    
                    <span class="hljs-comment">// permission was granted, yay! Do the</span>
                    <span class="hljs-comment">// contacts-related task you need to do.</span>
    
                } <span class="hljs-keyword">else</span> {
    
                    <span class="hljs-comment">// permission denied, boo! Disable the</span>
                    <span class="hljs-comment">// functionality that depends on this permission.</span>
                }
                <span class="hljs-keyword">return</span>;
            }
        }
    }</code>

    ok,对于权限的申请结果,首先验证requestCode定位到你的申请,然后验证grantResults对应于申请的结果,这里的数组对应于申请时的第二个权限字符串数组。如果你同时申请两个权限,那么grantResults的length就为2,分别记录你两个权限的申请结果。如果申请成功,就可以做你的事情了~

当然,到此我们的权限申请的不走,基本介绍就如上述。不过还有个API值得提一下:

<code class="language-java hljs  has-numbering"><span class="hljs-comment">// Should we show an explanation?</span>
<span class="hljs-keyword">if</span> (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
        Manifest.permission.READ_CONTACTS)) 
    <span class="hljs-comment">// Show an expanation to the user *asynchronously* -- don't block</span>
    <span class="hljs-comment">// this thread waiting for the user's response! After the user</span>
    <span class="hljs-comment">// sees the explanation, try again to request the permission.</span>

}</code>

这个API主要用于给用户一个申请权限的解释,该方法只有在用户在上一次已经拒绝过你的这个权限申请。也就是说,用户已经拒绝一次了,你又弹个授权框,你需要给用户一个解释,为什么要授权,则使用该方法。

那么将上述几个步骤结合到一起就是:

<code class="hljs javascript has-numbering"><span class="hljs-comment">// Here, thisActivity is the current activity</span>
<span class="hljs-keyword">if</span> (ContextCompat.checkSelfPermission(thisActivity,
                Manifest.permission.READ_CONTACTS)
        != PackageManager.PERMISSION_GRANTED) {

    <span class="hljs-comment">// Should we show an explanation?</span>
    <span class="hljs-keyword">if</span> (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.READ_CONTACTS)) {

        <span class="hljs-comment">// Show an expanation to the user *asynchronously* -- don't block</span>
        <span class="hljs-comment">// this thread waiting for the user's response! After the user</span>
        <span class="hljs-comment">// sees the explanation, try again to request the permission.</span>

    } <span class="hljs-keyword">else</span> {

        <span class="hljs-comment">// No explanation needed, we can request the permission.</span>

        ActivityCompat.requestPermissions(thisActivity,
                <span class="hljs-keyword">new</span> <span class="hljs-built_in">String</span>[]{Manifest.permission.READ_CONTACTS},
                MY_PERMISSIONS_REQUEST_READ_CONTACTS);

        <span class="hljs-comment">// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an</span>
        <span class="hljs-comment">// app-defined int constant. The callback method gets the</span>
        <span class="hljs-comment">// result of the request.</span>
    }
}</code>

四、简单的例子

这里写一个简单的例子,针对于运行时权限。相信大家在最开始接触Android的时候,都利用Intent实验了打电话、发短信等功能。

我们看看直接拨打电话在Android 6.x的设备上权限需要如何处理。

当然代码非常简单:

<code class="language-java hljs  has-numbering"><span class="hljs-keyword">package</span> com.zhy.android160217;

<span class="hljs-keyword">import</span> android.Manifest;
<span class="hljs-keyword">import</span> android.content.Intent;
<span class="hljs-keyword">import</span> android.content.pm.PackageManager;
<span class="hljs-keyword">import</span> android.net.Uri;
<span class="hljs-keyword">import</span> android.os.Bundle;
<span class="hljs-keyword">import</span> android.support.v4.app.ActivityCompat;
<span class="hljs-keyword">import</span> android.support.v4.content.ContextCompat;
<span class="hljs-keyword">import</span> android.support.v7.app.AppCompatActivity;
<span class="hljs-keyword">import</span> android.view.View;
<span class="hljs-keyword">import</span> android.widget.Toast;

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MainActivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">AppCompatActivity</span>
{</span>

    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> MY_PERMISSIONS_REQUEST_CALL_PHONE = <span class="hljs-number">1</span>;

    <span class="hljs-annotation">@Override</span>
    <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onCreate</span>(Bundle savedInstanceState)
    {
        <span class="hljs-keyword">super</span>.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testCall</span>(View view)
    {
        <span class="hljs-keyword">if</span> (ContextCompat.checkSelfPermission(<span class="hljs-keyword">this</span>,
                Manifest.permission.CALL_PHONE)
                != PackageManager.PERMISSION_GRANTED)
        {

            ActivityCompat.requestPermissions(<span class="hljs-keyword">this</span>,
                    <span class="hljs-keyword">new</span> String[]{Manifest.permission.CALL_PHONE},
                    MY_PERMISSIONS_REQUEST_CALL_PHONE);
        } <span class="hljs-keyword">else</span>
        {
            callPhone();
        }
    }

    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">callPhone</span>()
    {
        Intent intent = <span class="hljs-keyword">new</span> Intent(Intent.ACTION_CALL);
        Uri data = Uri.parse(<span class="hljs-string">"tel:"</span> + <span class="hljs-string">"10086"</span>);
        intent.setData(data);
        startActivity(intent);
    }

    <span class="hljs-annotation">@Override</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onRequestPermissionsResult</span>(<span class="hljs-keyword">int</span> requestCode, String[] permissions, <span class="hljs-keyword">int</span>[] grantResults)
    {

        <span class="hljs-keyword">if</span> (requestCode == MY_PERMISSIONS_REQUEST_CALL_PHONE)
        {
            <span class="hljs-keyword">if</span> (grantResults[<span class="hljs-number">0</span>] == PackageManager.PERMISSION_GRANTED)
            {
                callPhone();
            } <span class="hljs-keyword">else</span>
            {
                <span class="hljs-comment">// Permission Denied</span>
                Toast.makeText(MainActivity.<span class="hljs-keyword">this</span>, <span class="hljs-string">"Permission Denied"</span>, Toast.LENGTH_SHORT).show();
            }
            <span class="hljs-keyword">return</span>;
        }
        <span class="hljs-keyword">super</span>.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}
</code>

在Android 6.x上运行是,点击testCall,即会弹出授权窗口,如何你Allow则直接拨打电话,如果Denied则Toast弹出”Permission Denied”。

例子很简单,不过需要注意的是,对于Intent这种方式,很多情况下是不需要授权的甚至权限都不需要的,比如:你是到拨号界面而不是直接拨打电话,就不需要去申请权限;打开系统图库去选择照片;调用系统相机app去牌照等。更多请参考Consider Using an Intent
.

当然,上例也说明了并非所有的通过Intent的方式都不需要申请权限。一般情况下,你是通过Intent打开另一个app,让用户通过该app去做一些事情,你只关注结果(onActivityResult),那么权限是不需要你处理的,而是由打开的app去处理。

五、封装

(1)申请权限

虽然权限处理并不复杂,但是需要编写很多重复的代码,所以目前也有很多库对用法进行了封装,大家可以在github首页搜索:android permission,对比了几个库的使用方式,发现https://github.com/lovedise/PermissionGen这个库据我所见相比较而言使用算是比较简单的,那么就以这个库的源码为基础来讲解,大家有兴趣可以多看几个库的源码。

封装的代码很简单,正如大家的对上面的权限代码所见,没有特别复杂的地方。

对于申请权限的代码,原本的编写为:

<code class="language-java hljs  has-numbering"><span class="hljs-keyword">if</span> (ContextCompat.checkSelfPermission(<span class="hljs-keyword">this</span>,
                Manifest.permission.CALL_PHONE)
                != PackageManager.PERMISSION_GRANTED)
{

    ActivityCompat.requestPermissions(<span class="hljs-keyword">this</span>,
            <span class="hljs-keyword">new</span> String[]{Manifest.permission.CALL_PHONE},
            MY_PERMISSIONS_REQUEST_CALL_PHONE);
} <span class="hljs-keyword">else</span>
{
    callPhone();
}</code>

大家可以看到,对于其他的权限,其实申请的逻辑是类似的;唯一不同的肯定就是参数,那么看上面的代码,我们需要3个参数:Activity|Fragment权限字符串数组int型申请码

也就是说,我们只需要写个方法,接受这几个参数即可,然后逻辑是统一的。

<code class="language-java hljs  has-numbering"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">needPermission</span>(Fragment fragment, <span class="hljs-keyword">int</span> requestCode, String[] permissions)
{
    requestPermissions(fragment, requestCode, permissions);
}

<span class="hljs-annotation">@TargetApi</span>(value = Build.VERSION_CODES.M)
<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">requestPermissions</span>(Object object, <span class="hljs-keyword">int</span> requestCode, String[] permissions)
{
    <span class="hljs-keyword">if</span> (!Utils.isOverMarshmallow())
    {
        doExecuteSuccess(object, requestCode);
        <span class="hljs-keyword">return</span>;
    }
    List<String> deniedPermissions = Utils.findDeniedPermissions(getActivity(object), permissions);

    <span class="hljs-keyword">if</span> (deniedPermissions.size() > <span class="hljs-number">0</span>)
    {
        <span class="hljs-keyword">if</span> (object <span class="hljs-keyword">instanceof</span> Activity)
        {
            ((Activity) object).requestPermissions(deniedPermissions.toArray(<span class="hljs-keyword">new</span> String[deniedPermissions.size()]), requestCode);
        } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (object <span class="hljs-keyword">instanceof</span> Fragment)
        {
            ((Fragment) object).requestPermissions(deniedPermissions.toArray(<span class="hljs-keyword">new</span> String[deniedPermissions.size()]), requestCode);
        } <span class="hljs-keyword">else</span>
        {
            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> IllegalArgumentException(object.getClass().getName() + <span class="hljs-string">" is not supported"</span>);
        }

    } <span class="hljs-keyword">else</span>
    {
        doExecuteSuccess(object, requestCode);
    }
}</code>

Utils.findDeniedPermissions其实就是check没有授权的权限。

<code class="language-java hljs  has-numbering">#Utils
<span class="hljs-annotation">@TargetApi</span>(value = Build.VERSION_CODES.M)
<span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> List<String> <span class="hljs-title">findDeniedPermissions</span>(Activity activity, String... permission)
{
    List<String> denyPermissions = <span class="hljs-keyword">new</span> ArrayList<>();
    <span class="hljs-keyword">for</span> (String value : permission)
    {
        <span class="hljs-keyword">if</span> (activity.checkSelfPermission(value) != PackageManager.PERMISSION_GRANTED)
        {
            denyPermissions.add(value);
        }
    }
    <span class="hljs-keyword">return</span> denyPermissions;
}</code>

那么上述的逻辑就很清晰了,需要的3种参数传入,先去除已经申请的权限,然后开始申请权限。

ok,我相信上面代码,大家扫一眼就可以了解了。

(2)处理权限回调

对于回调,因为要根据是否授权去执行不同的事情,所以很多框架也需要些一连串的代码,或者和前面的申请代码耦合。不过这个框架还是比较方便的,也是我选择它来讲解的原因。

回调主要做的事情:

  1. 了解是否授权成功。
  2. 根据授权情况进行回调。

对于第一条逻辑都一样;对于第二条,因为涉及到两个分支,每个分支执行不同的方法。

对于第二条,很多框架选择将两个分支的方法在申请权限的时候进行注册,然后在回调中根据requestCode进行匹配执行,不过这样需要在针对每次申请进行对象管理。

不过这个框架采取了一种很有意思的做法,它利用注解去确定需要执行的方法,存在两个注解:

<code class="hljs ruby has-numbering"><span class="hljs-variable">@PermissionSuccess</span>(requestCode = <span class="hljs-number">100</span>)
<span class="hljs-variable">@PermissionFail</span>(requestCode = <span class="hljs-number">100</span>)</code>

利用反射根据授权情况+requestCode即可找到注解标注的方法,然后直接执行即可。

大致的代码为:

<code class="hljs java has-numbering"><span class="hljs-annotation">@Override</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onRequestPermissionsResult</span>(<span class="hljs-keyword">int</span> requestCode, String[] permissions,
      <span class="hljs-keyword">int</span>[] grantResults) {
    PermissionGen.onRequestPermissionsResult(<span class="hljs-keyword">this</span>, requestCode, permissions, grantResults);
}

<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">requestResult</span>(Object obj, <span class="hljs-keyword">int</span> requestCode, String[] permissions,
                                  <span class="hljs-keyword">int</span>[] grantResults)
{
    List<String> deniedPermissions = <span class="hljs-keyword">new</span> ArrayList<>();
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < grantResults.length; i++)
    {
        <span class="hljs-keyword">if</span> (grantResults[i] != PackageManager.PERMISSION_GRANTED)
        {
            deniedPermissions.add(permissions[i]);
        }
    }

    <span class="hljs-keyword">if</span> (deniedPermissions.size() > <span class="hljs-number">0</span>)
    {
        doExecuteFail(obj, requestCode);
    } <span class="hljs-keyword">else</span>
    {
        doExecuteSuccess(obj, requestCode);
    }
}</code>

首先根据grantResults进行判断成功还是失败,对于成功则:

<code class="language-java hljs  has-numbering"><span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">doExecuteSuccess</span>(Object activity, <span class="hljs-keyword">int</span> requestCode)
{
    Method executeMethod = Utils.findMethodWithRequestCode(activity.getClass(),
            PermissionSuccess.class, requestCode);

    executeMethod(activity, executeMethod);
}

#Utils
<span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <A extends Annotation> Method <span class="hljs-title">findMethodWithRequestCode</span>(Class clazz,  Class<A> annotation, <span class="hljs-keyword">int</span> requestCode)
{
    <span class="hljs-keyword">for</span> (Method method : clazz.getDeclaredMethods())
    {
        <span class="hljs-keyword">if</span> (method.isAnnotationPresent(annotation))
        {
            <span class="hljs-keyword">if</span> (isEqualRequestCodeFromAnntation(method, annotation, requestCode))
            {
                <span class="hljs-keyword">return</span> method;
            }
        }
    }
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;
}</code>

根据注解和requestCode找到方法,然后反射执行即可。失败的逻辑类似,不贴代码了。

ok,到此我们的运行时权限相对于早起版本的变化、特点、以及如何处理和封装都介绍完了。

不过对于上面讲解的库,肯定有人会说:运行时反射会影响效率,没错,不过我已经在上述代码的基础上将运行时注解改成Annotation Processor的方式了,即编译时注解,这样的话,就不存在反射损失效率的问题了。本来准备fork修改,然后PR,结果写完,改动太大,估计PR是不可能通过了,所以另起项目了,也方便后面的做一些扩展和维护。

详见库:https://github.com/hongyangAndroid/MPermissions.

六、MPermissions用法

对外的接口和PermissionGen基本一致,因为申请只需要三个参数,抛弃了使用原本类库的单例的方式,直接一个几个静态方法,简单整洁暴力。

贴一个用法:

<code class="hljs java has-numbering"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MainActivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">AppCompatActivity</span>
{</span>

    <span class="hljs-keyword">private</span> Button mBtnSdcard;
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> REQUECT_CODE_SDCARD = <span class="hljs-number">2</span>;

    <span class="hljs-annotation">@Override</span>
    <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onCreate</span>(Bundle savedInstanceState)
    {
        <span class="hljs-keyword">super</span>.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mBtnSdcard = (Button) findViewById(R.id.id_btn_sdcard);
        mBtnSdcard.setOnClickListener(<span class="hljs-keyword">new</span> View.OnClickListener()
        {
            <span class="hljs-annotation">@Override</span>
            <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onClick</span>(View v)
            {
                MPermissions.requestPermissions(MainActivity.<span class="hljs-keyword">this</span>, REQUECT_CODE_SDCARD, Manifest.permission.WRITE_EXTERNAL_STORAGE);
            }
        });
    }

    <span class="hljs-annotation">@Override</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onRequestPermissionsResult</span>(<span class="hljs-keyword">int</span> requestCode, String[] permissions, <span class="hljs-keyword">int</span>[] grantResults)
    {
        MPermissions.onRequestPermissionsResult(<span class="hljs-keyword">this</span>, requestCode, permissions, grantResults);
        <span class="hljs-keyword">super</span>.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }


    <span class="hljs-annotation">@PermissionGrant</span>(REQUECT_CODE_SDCARD)
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">requestSdcardSuccess</span>()
    {
        Toast.makeText(<span class="hljs-keyword">this</span>, <span class="hljs-string">"GRANT ACCESS SDCARD!"</span>, Toast.LENGTH_SHORT).show();
    }

    <span class="hljs-annotation">@PermissionDenied</span>(REQUECT_CODE_SDCARD)
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">requestSdcardFailed</span>()
    {
        Toast.makeText(<span class="hljs-keyword">this</span>, <span class="hljs-string">"DENY ACCESS SDCARD!"</span>, Toast.LENGTH_SHORT).show();
    }
}</code>

是不是简单明了~~对于onRequestPermissionsResult所有的Activity都是一致的,所以可以放到BaseActivity中去。此外,在Fragment中使用的方式一致,详见demo。

详见库:https://github.com/hongyangAndroid/MPermissions.

至于为什么不直接介绍MPermission的源码,因为主要涉及到Annotation Processor,所以以这种方式引出,后面考虑单篇博文介绍下我目前所会的编译时注解的相关做法以及API的使用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值