AsfK AsfK - 2 years ago 313
Android Question

Wait for user permission

I building an application that sampling GPS on starting.
As you probably know, permissions are requested during run-time from Android M and above.

So, in my case I'm starting with checking if permissions are needed, like this:

private void permissionForAndroidM()
if (Build.VERSION.SDK_INT > 22) {
String[] allPermissionNeeded = {

List<String> permissionNeeded = new ArrayList<>();
for (String permission : allPermissionNeeded)
if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED)
if (permissionNeeded.size() > 0) {
requestPermissions(permissionNeeded.toArray(new String[0]), 0);

but Android continuing running the code and asking for GPS data ( = crash, because the user didn't accept the permission request).

I find many solutions about waiting for user input (like using
, link, but it's cannot implemented in this case, because I'm not creating the dialog).

Bottom line, the question: How can I wait for user answer from the Android permission dialog ?

Answer Source

You can handle user response overriding method

public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)

from your activity.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download