Gissipi_453
Gissipi_453

Reputation: 1340

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

I have to get Runtime permissions from users. I'm passing requestCode as 200 in ActivityCompat.requestPermissions as you can see in code below.

But when the permissions are granted by the user, on onRequestPermissionsResult, the requestCode returned is 1. Here the requestCode returned should be 200, right ?

Can anyone explain why the requestCode returned is 1 in onRequestPermissionsResult and not 200 (the code I'm pasisng to requestPermissions) after the user has granted/accepted the runtime permissions?

        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

            }

        }
    }

Upvotes: 1

Views: 1453

Answers (2)

Rushikant Pawar
Rushikant Pawar

Reputation: 458

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 declarations :

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

Now in 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....
    }


}

Now check permissions result outside of 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 documentation on onRequestPermissionsResult Is explained here... Hope it helps you or someone else

The synopsis of onRequestPermissionsResult :

  1. requestCode is not for to pay attention on; its for application level
  2. permissions is the array of all permissions requested by this requestCode
  3. grantResults is an array of whatever user has done with the each and every single permission we have requested with this requestCode

requestCode is nothing to check for nor it has anything we have to do with

Upvotes: 4

Bhagwat K
Bhagwat K

Reputation: 3142

We can send(Execute) the multiple startActivity from activity's for result and in onActivityForResult method will receive multiple callbacks, so to identify the callback is for which request, that can be identified with requestCode, In-directly we can say requestCode as in id of the request that help us handle the response in onActivityResult method for particular request(staryActivityForResult).

Upvotes: -1

Related Questions