gpt4 book ai didi

android - 如何检查位置是否在 API 18 及以下版本上处于高优先级

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:20:38 26 4
gpt4 key购买 nike

试过这个:

String locationProviders = Settings.Secure.getString(getActivity().getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
Log.d(TAG_MAP, locationProviders);

输出为:gps,network

API 18 及以下的手机是否没有 HIGH_ACCURACY 优先级?

最佳答案

我假设您想知道使用 API 级别 19 中描述的 Settings.Secure.LOCATION_PROVIDERS_ALLOWED 与使用 Settings.Secure.LOCATION_MODE 有何不同,后者在 API 级别 19 中引入。

使用 Settings.Secure.LOCATION_MODE,您拥有这些值(您可能已经知道):

LOCATION_MODE_HIGH_ACCURACY, LOCATION_MODE_SENSORS_ONLY, LOCATION_MODE_BATTERY_SAVING, or LOCATION_MODE_OFF

您可以通过这种方式将这些值映射到 Settings.Secure.LOCATION_PROVIDERS_ALLOWED:

LOCATION_MODE_HIGH_ACCURACY : "gps,network"

LOCATION_MODE_SENSORS_ONLY : "gps"

LOCATION_MODE_BATTERY_SAVING : "network"

LOCATION_MODE_OFF : ""

请注意,在代码中,最好引用常量 LocationManager.GPS_PROVIDERLocationManager.NETWORK_PROVIDER 而不仅仅是“gps”和“network”。

使用 this answer作为引用,你可以这样做:

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;
}

关于android - 如何检查位置是否在 API 18 及以下版本上处于高优先级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29813924/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com