Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何等待用户在棉花糖中授予权限_Java_Android - Fatal编程技术网

Java 如何等待用户在棉花糖中授予权限

Java 如何等待用户在棉花糖中授予权限,java,android,Java,Android,我有一个应用程序,需要检查和请求多个权限(如果没有)。因此,当应用程序第一次运行时,它会跳过它之后的所有函数调用,即使在单击“允许访问”之后也是如此 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermissi

我有一个应用程序,需要检查和请求多个权限(如果没有)。因此,当应用程序第一次运行时,它会跳过它之后的所有函数调用,即使在单击“允许访问”之后也是如此

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

有没有办法在用户点击allow access之前不调用该函数

您必须考虑
requestPermissions
类似
startActivityForResult
。它只是启动用户授予/拒绝权限的UI

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

与onActivityResult类似,您必须覆盖onRequestPermissionsResult才能知道权限过程何时完成。您可以检查权限是被授予还是被拒绝,然后从那里执行您的逻辑。

我认为您应该参考这个。