首页 > 其他分享 >开启定位权限还是定位失败

开启定位权限还是定位失败

时间:2023-01-16 10:22:16浏览次数:37  
标签:定位 return Secure Settings 开启 LOCATION 权限

1.除了要开启定位权限,还要开启定位服务,也就是手机设置里面的位置信息,安卓9以前都是默认开启的,安卓10以后放到桌面下拉框了,如果位置信息没有开启,也是没办法定位的


2.操作

2.1 判断手机定位服务是否开启

/**
* 判断定位服务是否开启
*
* @param context 上下文
* @return true:开启;false:未开启
*/
public static boolean isLocationEnabled(Context context) {
int locationMode = 0;
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 false;
}
return locationMode != Settings.Secure.LOCATION_MODE_OFF;
} else {
locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
return !TextUtils.isEmpty(locationProviders);
}
}

2.2 跳转到系统的定位服务页面

startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
————————————————

标签:定位,return,Secure,Settings,开启,LOCATION,权限
From: https://www.cnblogs.com/wzqnxd/p/17054805.html

相关文章