异步显示权限说明

show permission explanation asynchronisly

从 Android 6.0 开始,在运行时请求权限,而不是在安装之前。

Android official doc推荐代码如下:

// 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.
        ...
    }
}

我对上面示例代码中的一件事感到困惑,这就是为什么上面的评论说“向用户显示解释异步 “?这是一个惯例吗?我的意思是,如果我只是打算弹出一个对话框来解释为什么需要权限,我认为不需要异步弹出对话框。我只是不明白为什么 google 在那里推荐异步代码。

这是否表明 google 不希望开发人员弹出对话框出现,而是执行一些繁重的操作?嗯...无论如何,对此很困惑。

Is it a convention to follow?

这是所有 Android 应用程序开发中的标准概念,尤其是在处理 UI 时。这也是非常需要的,因为在 Android.

中没有用于同步 UIs 的 API

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.

您在 Android 中弹出的每个对话框都是异步弹出的。当您调用 show() 时,对话框不会在 show() return 秒后出现。 计划 出现,它会在您 return 控制主应用程序线程后的某个时间出现。这就是 Google 在这里的 "asynchronous" 的意思。

A 同步 UI 如果调用 show() 显示对话框 阻止进一步执行您的代码 直到对话框消失,这样您就可以知道用户在 show().

之后的下一条语句中对该对话框做了什么