-1

I have a method named getPermisions that uses a helper method named hasPermissions. I call getPermissions() in the onCreate method as follows . . .

getPermissions();
//code here uses these permissions
camera = Camera.open();  //but this code executes async to getPermissions method

Code for getPermissions is as follows . . .

private  boolean hasPermissions(Context context, String... permissions) {
    if (context != null && permissions != null) {
        for (String permission : permissions) {
            if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
    }
    return true;
}
private void getPermissions()
{
    //before we display setup dialog we must get permissions . . .

            int PERMISSION_ALL = 1;
            String[] PERMISSIONS = {
                    android.Manifest.permission.READ_PHONE_STATE,
                    android.Manifest.permission.ACCESS_FINE_LOCATION,
                    android.Manifest.permission.CAMERA
            };

            if(!hasPermissions(this, PERMISSIONS)){
                ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
            }

}

The code works perfectly if I am stepping through but when actually running the app the getPermissions() code runs asynchronously to the Main UI thread. So when I call getPermissions() the main thread continues to run using permissions I'm still asking for.

I need to hold up the main thread while the user answers all three dialog boxes. How best to do that?

Dean Blakely
  • 3,535
  • 11
  • 51
  • 83
  • so, if I understand, hasPermission is executing first before you grant the permission, so therefore when you grant the permission you need to check hasPermission but it already executed, I'm right ? – Gastón Saillén Sep 05 '18 at 21:33
  • no. hasPermission is just a helper class to getPermissions and is not really part of this problem. When I call getPermissions() the instruction right after that executes which uses the camera. – Dean Blakely Sep 05 '18 at 21:44
  • now I understand, thanks :) – Gastón Saillén Sep 05 '18 at 21:48

1 Answers1

1

So when I call getPermissions() the main thread continues to run using permissions I'm still asking for.

Well, requestPermissions() works asynchronously, if that's what you mean. Under the covers, it is using startActivityForResult() to display the system-supplied permission dialog.

I need to hold up the main thread while the user answers all three dialog boxes

That's not possible. You will never get the permission dialog, let alone the result.

It's also not necessary.

In a nutshell:

  • Put your code that relies upon these permissions in a method
  • Call that method if hasPermissions() returns true
  • Call getPermissions() if hasPermissions() returns false
  • Call that method from onRequestPermissionsResult(), if the user granted you your requested permissions

If you do not like that, there are a few dozen libraries that aim to offer simpler patterns for using runtime permissions. Perhaps one of them will pique your interest.

The use of runtime permissions is covered in the documentation.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491