Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 = {                 Manifest.permission.WRITE_EXTERNAL_STORAGE,                 Manifest.permission.ACCESS_FINE_LOCATION,                 Manifest.permission.ACCESS_COARSE_LOCATION,                 Manifest.permission.CAMERA,                 Manifest.permission.RECORD_AUDIO};          List<String> permissionNeeded = new ArrayList<>();         for (String permission : allPermissionNeeded)             if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED)                 permissionNeeded.add(permission);         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 DialogInterface.OnClickListener, 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 ?

like image 325
AsfK Avatar asked May 30 '16 13:05

AsfK


People also ask

How do I check if permission is granted Android?

To check if the user has already granted your app a particular permission, pass that permission into the ContextCompat. checkSelfPermission() method. This method returns either PERMISSION_GRANTED or PERMISSION_DENIED , depending on whether your app has the permission.

How do you ask permission runtime again if the user deny for the first time?

Request permissions If the permission is not granted, we need to ask for permission to access certain features like camera, storage, etc. To ask permission, we need to use ActivityCompat. requestPermission() method with the permission name and the request code.


1 Answers

You can handle user response overriding method

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

from your activity.

like image 85
Valentin Yuryev Avatar answered Sep 22 '22 04:09

Valentin Yuryev