-1

I've seen SO question Can you request permissions synchronously in Android Marshmallow (API 23)'s runtime permissions model?. The answer is no.

Hence, I added a code as below (simplified version):

public class MyActivity ... {
    private boolean hasGotPermissionRequestResult = false;

    @Override
    public void onCreate(...) {
        if (ContextCompat.checkSelfPermission(...) == PackageManager.PERMISSION_DENIED) {
            ActivityCompat.requestPermission(...);
            while (!hasGotPermissionRequestResult) {}
        }
    }

    @Override
    public void onRequestPermissionResult(...) {
        // whether granted or not
        hasGotPermissionRequestResult = true;
    }
}

However, I am not sure whether my approach is nice, safe and efficient.

Community
  • 1
  • 1
Jeon
  • 4,000
  • 4
  • 28
  • 73
  • 1
    `while (!hasGotPermissionRequestResult) {}` in `onCreate` ? ... basically it is not nice neither safe neither efficient ... and prolly will not work – Selvin Sep 30 '16 at 14:25
  • Rule of thumb: never use busy waiting loops. Especially not on the UI thread – Tim Sep 30 '16 at 14:27

1 Answers1

0

Well, what you trying to achieve simply ain't possible, however there are several ways to overcome this:

  1. Only trigger the method you wanna call when the permission is granted.
  2. If you'd like to make the User only uses your app because that particular permission is so important that your app will not function without it, then use an educated screen to tell the users why you would want to use this permission in an intro screen kinda way.

I've created a library just for this kind of scenario where the library simplify the Permissions for you, it can be also used as a stand Alone Activity that has an Intro to your permission. you could check it out in Github PermissionHelper Github

Community
  • 1
  • 1
Kosh
  • 6,140
  • 3
  • 36
  • 67