show permission explanation asynchronisly

Starting with Android 6.0, permissions are requested at runtime, and not before the installation.

Android official doc recommended the following code:

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
                Manifest.permission.READ_CONTACTS)
        != PackageManager.PERMISSION_GRANTED) {

    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.READ_CONTACTS)) {

        // Show an explanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.

    } else {
        // No explanation needed, we can request the permission.
        ...
    }
}

I am confused about one thing in above sample code, that's why the comment above says " Show an explanation to the user asynchronously "? Is it a convention to follow? I mean if I just plan to pop up a dialog to explain why the permission is needed, I don't see the need to popup the dialog asynchronisly. I just don't understand why google recommend asynchronous code there.

Does it indicate google doesn't want developer popup dialog there but do some heavy action? hmm...anyway, quite confusing on this.


Is it a convention to follow?

It is a standard concept in all Android app development, particularly when dealing with a UI. It's also pretty much required, as there are no APIs for synchronous UIs in Android.

I mean if I just plan to pop up a dialog to explain why the permission is needed, I don't see the need to popup the dialog asynchronisly.

Every dialog that you have ever popped up in Android has been popped up asynchronously. When you call show() , the dialog does not appear by the time show() returns. It is scheduled to appear, and it will appear sometime after you return control to the main application thread. This is what Google means by "asynchronous" here.

A synchronous UI would be if calling show() to display a dialog blocked further execution of your code until the dialog went away, so that you knew what the user did with that dialog on the next statement following show() .

链接地址: http://www.djcxy.com/p/37942.html

上一篇: 单位的最佳策略是什么?

下一篇: 不同步地显示权限说明