Android M 权限授予回调

Jak*_*ake 6 permissions android android-6.0-marshmallow

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

public void onRequestPermissionsResult (int requestCode, 
               String[] permissions, int[] grantResults) { }
Run Code Online (Sandbox Code Playgroud)

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

Dwi*_* Ji 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;
        }
Run Code Online (Sandbox Code Playgroud)

打回来

 @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
        }
    }
Run Code Online (Sandbox Code Playgroud)


Com*_*are 4

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

例如,假设我有:

  private static final String[] PERMS_ALL={
    CAMERA,
    WRITE_EXTERNAL_STORAGE
  };
Run Code Online (Sandbox Code Playgroud)

我打电话给:

requestPermissions(PERMS_ALL, RESULT_PERMS_ALL);
Run Code Online (Sandbox Code Playgroud)

CAMERA并且WRITE_EXTERNAL_STORAGE位于不同的权限组中。系统将按组提示用户授予或拒绝权限。他们根据权限提供结果,因为我们请求权限(而不是组)。但用户可以:

  • 授予两者
  • 否认两者
  • 授予CAMERA但不WRITE_EXTERNAL_STORAGE
  • 授予WRITE_EXTERNAL_STORAGE但不CAMERA

因此,他们向我们提供了完整的结果清单。

就我个人而言,我不会使用这些结果和 call checkSelfPermission(),只是为了防止出现一些奇怪的竞争条件,我onRequestPermissionResult()暂时不会被调用,并且用户首先通过“设置”改变主意。