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?