Android M权限授予回拨

时间:2015-09-26 15:55:50

标签: android permissions android-6.0-marshmallow

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

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

我想问为什么权限和grantResults参数定义为数组?我知道可以使用requestPermissions()同时询问多个权限,但如果请求代码用于请求的权限集,那么只需要一个整数grantResults就足够了(不确定权限参数)?

2 个答案:

答案 0 :(得分:4)

为我工作

检查并请求许可

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

答案 1 :(得分:2)

不,因为用户可以独立授予或拒绝您请求的任何权限。

例如,假设我有:

  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()调用一段时间而且用户会改变主意首先通过设置。