简体   繁体   中英

How do I recognize GRANTED and UNACCEPTED of permissions?

I use this code to recognize permissions in onCreate:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
     requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, 100);

And in activity I use this code snippet to check, allow or deny the permissions :

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
  switch (requestCode) {
     case 100: {
         if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
             Toast.makeText(getApplicationContext(), "Ok", Toast.LENGTH_SHORT).show();
         else
             Toast.makeText(getApplicationContext(), "No", Toast.LENGTH_SHORT).show();
     }
  }
}

and in Manifest.xml :

<!-- *****  Uses Permission  ***** -->
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

But it always returns the value of zero in the code, both when it is verified and when it is rejected.

How do I tell if the user has verified or denied?

I have tried ur example and it gives proper result with 0 and -1. 0 for Granted and -1 for Denied .

that might be device problem and don't use emulator for this, otherwise your code works just fine. here is my snapshot:

01-13 13:00:29.134 30051-30051/com.usense.netsense I/get_permission:  permissions[0]= android.permission.READ_PHONE_STATE grantResults[0]= 0
01-13 13:00:29.134 30051-30051/com.usense.netsense I/get_permission:  permissions[1]= android.permission.WRITE_EXTERNAL_STORAGE grantResults[1]= -1
01-13 13:00:29.134 30051-30051/com.usense.netsense I/get_permission:  permissions[2]= android.permission.READ_EXTERNAL_STORAGE grantResults[2]= -1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM