在AndroidManifest.xml文件中,只需声明ACCESS_FINE_LOCATION权限即可自动获取ACCESS_COARSE_LOCATION权限。示例如下:
在代码中使用时,可以检查ACCESS_FINE_LOCATION权限是否被授予来确定ACCESS_COARSE_LOCATION权限是否可用。示例如下:
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
// ACCESS_FINE_LOCATION permission is granted, ACCESS_COARSE_LOCATION is also available
} else {
// ACCESS_FINE_LOCATION permission is not granted, request it (which also includes ACCESS_COARSE_LOCATION)
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
REQUEST_LOCATION_PERMISSION);
}