android - 检查全球位置访问权限
问题描述
在 Android 中,我可以检查我的应用是否具有 Manifest 要求的位置权限,如下所示:
if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// show alert
}
但是,当用户转到“Android 设置”>“安全和隐私”>“位置访问”并将其全局关闭时,该警报不会显示。有没有办法检测该全局权限是否设置为关闭?
解决方案
如果启用了全局位置,则此函数返回
public boolean isLocationEnabled() {
int locationMode = Settings.Secure.LOCATION_MODE_OFF;
String locationProviders;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
try {
locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
return locationMode != Settings.Secure.LOCATION_MODE_OFF;
} else {
locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
return !TextUtils.isEmpty(locationProviders);
}
}
推荐阅读
- javascript - jquery 中是否有任何函数可以用新代码替换整个 html 代码?
- plugins - 我可以同时为 Outlook 插件和 gmail 插件创建单个插件并构建吗?
- swift - 获取行列中的单元格(Cocoa)
- angular - 如何创建富文本框,可以用 Angular 组件替换文本
- xml - 按日期创建自动操作 - Odoo 10
- python - API 文档中未记录使用 drf-extensions 的嵌套路由
- c - 尝试将值转换为 C 中的 2s 补码
- vuejs2 - 如何在 Vuetify 中禁用文字值?
- aws-java-sdk - S3 始终返回错误代码:NoSuchKey 即使使用不正确的存储桶名称,而不是一些详细说明存储桶的特定错误代码
- asp.net-mvc-4 - 编辑器显示 html 标签而不是数据的问题