如何在Marshmallow中等待用户授权权限

3

我有一个应用程序,需要检查并请求多个权限。因此,当应用程序第一次运行时,即使点击允许访问,它也会跳过所有在其后的函数调用。

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
        != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    // Check Permissions Now
    ActivityCompat.requestPermissions(
            this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
            REQUEST_CODE_LOCATION);
}
// Will not be called the first time
functionCall()

有没有办法在用户允许访问之前不调用该函数?

我认为你应该参考这个链接:https://dev59.com/j10a5IYBdhLWcg3wF1UL - niravmodi
1个回答

3

你需要将 requestPermissions 看作是 startActivityForResult 。它仅启动用户界面以授予/拒绝权限。

就像 onActivityResult 一样,您必须重写 onRequestPermissionsResult 以了解权限处理何时完成。您可以检查权限是否已被授予或拒绝,并从那里执行逻辑。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接