Android M权限授予回调

7

我正在测试Android M开发预览版上的权限系统。我有一个关于回调函数的问题。Activity类有一个新的API:

public void onRequestPermissionsResult (int requestCode, 
               String[] permissions, int[] grantResults) { }

我想问一下为什么权限和grantResults参数被定义为数组?我知道可以使用requestPermissions()同时请求多个权限,但如果使用请求代码来请求权限集合,只有一个整数的grantResults(不确定权限参数)是否就足够了呢?


除了答案中提到的内容,我建议使用这个库来简化流程 https://github.com/kayvannj/PermissionUtil - Kayvan N
类似上面提到的库,我在 https://github.com/anthonycr/Grant 找到了一个辅助库,感谢两位开发者! :) - computingfreak
2个回答

5

请为我工作

检查并请求权限

if ( ContextCompat.checkSelfPermission( this, android.Manifest.permission.ACCESS_COARSE_LOCATION ) != PackageManager.PERMISSION_GRANTED ) {


            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    android.Manifest.permission.ACCESS_COARSE_LOCATION )) {

                // Show an expanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.

            } else {

                // No explanation needed, we can request the permission.

                ActivityCompat.requestPermissions(this,
                        new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION },
                        MY_PERMISSIONS_REQUEST_ACCESS_LOCATION);

                // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
                // app-defined int constant. The callback method gets the
                // result of the request.
            }

            return;
        }

回调函数

 @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_ACCESS_LOCATION: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    // permission was granted, yay! Do the
                    // contacts-related task you need to do.
                    moveToNextActivity();

                } else {

                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                }
                return;
            }

            // other 'case' lines to check for other
            // permissions this app might request
        }
    }

使用以下方式检查是否拒绝了任何权限:if (grantResults.length > 0 && !Arrays.asList(grantResults).contains(PackageManager.PERMISSION_DENIED)) - Pier Betos

4
不可以,因为用户可以独立地授予或拒绝您请求的任何权限。
例如,假设我有以下内容:
  private static final String[] PERMS_ALL={
    CAMERA,
    WRITE_EXTERNAL_STORAGE
  };

我称之为:

requestPermissions(PERMS_ALL, RESULT_PERMS_ALL);
CAMERAWRITE_EXTERNAL_STORAGE属于不同的权限组。用户将分别被提示授予权限或拒绝权限。我们按照权限而非权限组请求权限,因此他们会按照权限提供结果。但用户可以:

  • 同时授予两个权限
  • 拒绝两个权限
  • 授予权限CAMERA但不授予权限WRITE_EXTERNAL_STORAGE
  • 授予权限WRITE_EXTERNAL_STORAGE但不授予权限CAMERA

因此,他们为我们提供了完整的结果。

个人而言,我不使用这些结果并调用checkSelfPermission(),以防一些奇怪的竞争条件,其中我一时半刻未被调用onRequestPermissionResult(),而用户先通过设置改变了主意。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接