Java – How to wait for a user to grant permission in Marshmallow

How to wait for a user to grant permission in Marshmallow… here is a solution to the problem.

How to wait for a user to grant permission in Marshmallow

I have

an app and if I don’t have it, I need to check and request multiple permissions. Therefore, when the application runs for the first time, it skips all subsequent function calls, even after clicking Allow Access

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
        != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
     Check Permissions Now
    ActivityCompat.requestPermissions(
            this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
            REQUEST_CODE_LOCATION);
}
 Will not be called the first time
functionCall()

Is there a way not to call the function until the user clicks Allow access

Solution

You must treat requestPermissions as startActivityForResult. It simply launches the UI for the user to grant/deny permissions.

As with onActivityResult, you must override onRequestPermissionsResult to know when permission processing is complete. You can check if permissions are granted or denied and execute your logic from there.

Related Problems and Solutions