How to ask for user permissions in Android 6

为君一笑 提交于 2019-12-06 15:45:58

You shouldn't ask for permissions in AsyncTask. In fact you shouldn't even retrieve deviceId or some information in it - pass it as a parameter.

Settings of each application has list of 'dangerous' permissions with their status. If you app targeting sdk 23, they're all disabled by default. So, when you use code which requires such permission, you need to check if it's granted and if not - ask for it.

The simplies way is to create utility class for permissions like this:

public final class PermissionUtils {

    private PermissionUtils() {
    }

    public static boolean checkPermissions(Context context, String... permissions) {
        for (String permission : permissions) {
            if (!checkPermission(context, permission)) {
                return false;
            }
        }
        return true;
    }

    public static boolean checkPermission(Context context, String permission) {
        return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
    }

    public static boolean isDeviceInfoGranted(Context context) {
        return checkPermission(context, Manifest.permission.READ_PHONE_STATE);
    }

    public static void requestPermissions(Object o, int permissionId, String... permissions) {
        if (o instanceof Fragment) {
            FragmentCompat.requestPermissions((Fragment) o, permissions, permissionId);
        } else if (o instanceof Activity) {
            ActivityCompat.requestPermissions((AppCompatActivity) o, permissions, permissionId);
        }
    }
}

And then use it like this: (in your Fragment / Activity)

if (PermissionUtils.isDeviceInfoGranted(this)) {
    //get deviceId and proccess your code
} else {
    String[] permissions = new String[]{READ_PHONE_STATE};
    PermissionUtils.requestPermissions(this, PHONE_STATE_PERMISSION_ID, permissions);
}

Override handler for permission result in your AppCompatActivity or implement OnRequestPermissionsResultCallback interface:

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
        case PHONE_STATE_PERMISSION_ID:
            boolean granted = grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED;
            if (granted) {
                //get deviceId and proccess your code
            } else {
                //nobody knows what to do
            }
            break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

As you see, we still have a problem - what to do, if user denied permission? As a variant, we can show explaining dialog. Or (may be better) ask for permissions only after onboarding screens.

First of all you must implement the following method in your activity/fragment:

@Override
public void onRequestPermissionsResult(int requestCode,@NonNull String permissions[], @NonNull int[] grantResults) {
    switch (requestCode) {
        case Permissions.READ_CONTACTS: { //Permissions.READ_CONTACTS is an int constant
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                getContactsFromPhone();
            }
        }
    }
}

Then in ur onclick to get this contacts you should ask if you have this permission:

    if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(activity,
                new String[]{Manifest.permission.READ_CONTACTS}, Permissions.READ_CONTACTS); //Permissions.READ_CONTACTS is an int constant
        return false;
    } else {
       getContactsFromPhone();
    }
noktigula

1) Yes, you should check that permission is granted every time you called these functions.
2) You can check and request permissions from main thread. I think that check and request permissions from AsyncTask isn't good way, because permission request is a dialog, and you will have to wait asynctask thread until you get user response. Try to check permission from main thread, get user response, and then, if permission granted, start your asynctask.

Dexter is an Android library that simplifies the process of requesting permissions at runtime.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!