Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 771
Leem.fin Avatar asked Jan 07 '17 12:01

Leem.fin


People also ask

What are manifest permissions?

The Android manifest file helps to declare the permissions that an app must have to access data from other apps. The Android manifest file also specifies the app's package name that helps the Android SDK while building the app.

What are SMS permissions?

Most of these are pretty self-explanatory---for instance, the SMS permission lets apps read and send text messages---but you'll see descriptions at the top of each page if you're not sure. Tap a permission and under Allowed, you'll see every app that you've approved to use that function.


1 Answers

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().

like image 82
CommonsWare Avatar answered Oct 12 '22 23:10

CommonsWare