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才能知道权限过程何时完成。您可以检查权限是被授予还是被拒绝,然后从那里执行您的逻辑。我认为您应该参考这个。