试过这个:
String locationProviders = Settings.Secure.getString(getActivity().getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
Log.d(TAG_MAP, locationProviders);
输出为:GPS,网络
API 18 及更低版本的手机是否没有HIGH_ACCURACY优先级?
我假设您想知道使用 API 级别 19 中已弃用的 Settings.Secure.LOCATION_PROVIDERS_ALLOWED
与使用 API 级别 19 中引入的 Settings.Secure.LOCATION_MODE
有何不同。
使用 Settings.Secure.LOCATION_MODE
,您有这些值(您可能已经知道这一点):
LOCATION_MODE_HIGH_ACCURACY, LOCATION_MODE_SENSORS_ONLY, LOCATION_MODE_BATTERY_SAVING或LOCATION_MODE_OFF
您可以通过以下方式将这些值映射到Settings.Secure.LOCATION_PROVIDERS_ALLOWED
:
LOCATION_MODE_HIGH_ACCURACY : "GPS,网络"
LOCATION_MODE_SENSORS_ONLY : "全球定位系统"
LOCATION_MODE_BATTERY_SAVING:"网络"
LOCATION_MODE_OFF : "
请注意,在代码中,最好引用常量LocationManager.GPS_PROVIDER
和LocationManager.NETWORK_PROVIDER
,而不仅仅是"gps"和"network"。
使用此答案作为参考,您可以执行以下操作:
public static int getLocationMode(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 (SettingNotFoundException e) {
e.printStackTrace();
}
} else {
locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (TextUtils.isEmpty(locationProviders)) {
locationMode = Settings.Secure.LOCATION_MODE_OFF;
}
else if (locationProviders.contains(LocationManager.GPS_PROVIDER) && locationProviders.contains(LocationManager.NETWORK_PROVIDER)) {
locationMode = Settings.Secure.LOCATION_MODE_HIGH_ACCURACY;
}
else if (locationProviders.contains(LocationManager.GPS_PROVIDER)) {
locationMode = Settings.Secure.LOCATION_MODE_SENSORS_ONLY;
}
else if (locationProviders.contains(LocationManager.NETWORK_PROVIDER)) {
locationMode = Settings.Secure.LOCATION_MODE_BATTERY_SAVING;
}
}
return locationMode;
}
此功能。我在 API 21 和 API 16 上进行了测试
public static boolean checkHighAccuracyLocationMode(Context context) {
int locationMode = 0;
String locationProviders;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
//Equal or higher than API 19/KitKat
try {
locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
if (locationMode == Settings.Secure.LOCATION_MODE_HIGH_ACCURACY){
return true;
}
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
}else{
//Lower than API 19
locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (locationProviders.contains(LocationManager.GPS_PROVIDER) && locationProviders.contains(LocationManager.NETWORK_PROVIDER)){
return true;
}
}
return false;
}
请记住,HIGH_ACCURACY模式意味着用户启用了 GPS 和网络提供商。否则,此函数将返回 false。
试试这个Settings.Secure.LOCATION_MODE
String gpsStatus = android.provider.Settings.Secure.getString(mContext.getContentResolver(), Settings.Secure.LOCATION_MODE);
您可以使用此检查
if (Build.VERSION.SDK_INT >= 21 &&
ActivityCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Log.e("gpsStatus", "LOCATION PERMISSION NOT GRANTED");
} else {
Log.d("gpsStatus", "LOCATION PERMISSION GRANTED");
if (gpsStatus.indexOf("gps", 0) < 3) {
Log.d("gpsCheck", "DEVICE LOCATION PERMISSION IS GRANTED");
} else {
Log.e("gpsCheck", "DEVICE LOCATION PERMISSION NOT GRANTED");
}
}