Home Lack of distinction to confirm and deny runtime permission
Reply: 1

Lack of distinction to confirm and deny runtime permission

Majid ahmadi
1#
Majid ahmadi Published in 2018-01-13 06:27:47Z

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,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            Manifest.permission.READ_EXTERNAL_STORAGE}, 250);

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

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {

    Log.i("get_permission", "onRequestPermissionsResult: grantResults.length= " + requestCode + " permissions= " + permissions.length + "  grantResults= " + grantResults.length);
    for (int i = 0; i < permissions.length; i++)
        Log.i("get_permission", " permissions[" + i + "]= " + permissions[i] + " grantResults[" + i + "]= " + grantResults[i]);

    switch (requestCode) {
        case 250: {
            if (grantResults.length > 0 && (grantResults[0] + grantResults[1] + grantResults[2]) == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(getApplicationContext(), "Ok", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getApplicationContext(), "No", Toast.LENGTH_SHORT).show();
                startActivityForResult(new Intent(this, RequiredPermission.class), 250);
            }
        }
    }
}

and in Manifest.xml :

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

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

Log is :

01-13 09:36:09.153 12637-12637/com.example.test I/get_permission: onRequestPermissionsResult: grantResults.length= 250 permissions= 3  grantResults= 3
01-13 09:36:09.153 12637-12637/com.example.test I/get_permission:  permissions[0]= android.permission.READ_PHONE_STATE grantResults[0]= 0
01-13 09:36:09.153 12637-12637/com.example.test I/get_permission:  permissions[1]= android.permission.WRITE_EXTERNAL_STORAGE grantResults[1]= 0
01-13 09:36:09.163 12637-12637/com.example.test I/get_permission:  permissions[2]= android.permission.READ_EXTERNAL_STORAGE grantResults[2]= 0

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

Pratim P
2#
Pratim P Reply to 2018-01-13 07:33:56Z

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
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.299386 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO