Wait for user permission
<p>I building an application that sampling GPS on starting.
As you probably know, permissions are requested during run-time from Android M and above.</p>

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

<pre><code>private void permissionForAndroidM()
{
    if (Build.VERSION.SDK_INT &gt; 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&lt;String&gt; permissionNeeded = new ArrayList&lt;&gt;();
        for (String permission : allPermissionNeeded)
            if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED)
                permissionNeeded.add(permission);
        if (permissionNeeded.size() &gt; 0) {
            requestPermissions(permissionNeeded.toArray(new String[0]), 0);
        }
    }
}
</code></pre>

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

<p>I find many solutions about waiting for user input (like using <code>DialogInterface.OnClickListener</code>, <a href="https://stackoverflow.com/questions/9630681/android-wait-the-main-thread-while-a-dialog-gets-input-in-a-separate-thread">link</a>, but it's cannot implemented in this case, because I'm not creating the dialog).</p>

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