为什么在用户授予运行时权限后,onRequestPermissionsResult 中返回的requestCode 为1?

Why is requestCode returned in onRequestPermissionsResult is 1 after user has granted runtime permissions?

我必须获得用户的运行时权限。我在 ActivityCompat.requestPermissions 中将 requestCode 作为 200 传递,您可以在下面的代码中看到。

但是当用户授予权限时,在 onRequestPermissionsResult 上,返回的 requestCode1。这里返回的requestCode应该是200吧?

谁能解释为什么 onRequestPermissionsResult 中返回的 requestCode 是 1 而不是 200 (在用户拥有 granted/accepted 运行时权限后,我将代码传递给 requestPermissions?

        var mRuntimePermissions = RuntimePermissions(permissions, this)
        var gotPermissions = mRuntimePermissions.hasPermissions()

        if(gotPermissions == true){

            startActivity(Intent(this, MainActivity::class.java))
        }
        else{
            ActivityCompat.requestPermissions(this, permissions, 200);
        }


override fun onRequestPermissionsResult(requestCode: Int,
                 permissions: Array<String>, grantResults: IntArray) {
        when (requestCode) {

            // check requestCode and do something

            }

        }
    }

我们可以从 activity's 发送(执行)多个 startActivity 作为结果,在 onActivityForResult 方法中将接收多个回调,因此要确定回调是针对哪个请求的,即可以用 requestCode 来标识,我们可以直接说 requestCode 作为请求的 ID,帮助我们处理特定请求的 onActivityResult 方法中的响应(staryActivityForResult) .

I do not know how much it will help you; as it is not in kotlin. But i have implemented it in many projects in following manner.. Hope it helps you to get at least some clues.

Class 声明:

private static String[] PERMISSIONS = {Manifest.permission.READ_PHONE_STATE, Manifest.permission.PROCESS_OUTGOING_CALLS};
public int ALL_PERMISSIONS = 0;
public boolean CheckNoPermission = false;

现在在 OnCreate 中:

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
{
    ALL_PERMISSIONS  = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE);
    ALL_PERMISSIONS  = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.PROCESS_OUTGOING_CALLS);

    // its below marshmallow
    // no need to ask permissions in this case
    // do the things here directly

    // START YOUR ACTIVITY HERE....
}
else
{
    // its marshmallow or above
    // have to ask permissions in this case

    // lets check if user already allowed the permissions in previous run

    for(int x = 0; x < PERMISSIONS.length; x++)
    {
        String ThisPermission = PERMISSIONS[x];

        if (checkSelfPermission(ThisPermission) != PackageManager.PERMISSION_GRANTED)
        {
             CheckNoPermission = true;
             break;
        }
    }

    // even if any permission is not granted then re-request all permissions; 
    // re-requesting all permissions will only request denied permissions only

    if(CheckNoPermission)
    {
          // request all permissions one by one in array

          ActivityCompat.requestPermissions(MainActivity.this, PERMISSIONS, ALL_PERMISSIONS);
    }
    else
    {
         // user has already granted all the permissions in previous run
         // do the things here directly

         // START YOUR ACTIVITY HERE....
    }


}

现在在 onCreate 之外检查权限结果:

// if permissions are requested; check for user approvals :

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
{
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (grantResults.length == 2)
    {
        Boolean IsAllPermissions = true;

        // Yes need to check all permissions again as user may have denied any of the permission

        for(int p = 0; p < grantResults.length; p++)
        {
            if (grantResults[p] != PackageManager.PERMISSION_GRANTED)
            {
                IsAllPermissions = false;
                break;
            }
        }

        if(IsAllPermissions)
        {
            // user has granted all the permissions in this run
            // do the things here ...

            // START YOUR ACTIVITY HERE....
        }
        else
        {
            // user has not granted all the permissions in this run
            // so do what you want to do here in such case ...
        }
     }
     else
     {
         // missmatch in number of permissions in result
     }
 }

Android 关于 onRequestPermissionsResult 的文档 Is explained here... 希望对您或其他人有所帮助

onRequestPermissionsResult的剧情简介:

  1. requestCode不是为了注意;其应用级别
  2. permissions是这个requestCode
  3. 请求的所有权限的数组
  4. grantResults 是一个数组,其中包含用户对我们使用此 requestCode
  5. 请求的每个权限所做的任何操作

requestCode 无需检查,也与我们无关