以编程方式将定位模式更改为高精度 Android [英] Change Location Mode to High Accuracy Programmatically Android

查看:26
本文介绍了以编程方式将定位模式更改为高精度 Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在位置设置选项下获取用户在三种模式中选择的位置模式的信息,即

Is it possible to get the information on the location mode which the user has selected among the three modes under the location settings options i.e

1.高精度

2.省电

3.仅GPS

我想以编程方式检查用户是否选择了高精度模式,如果没有,则自动启用它.是否可以 ?请指教.

I want to programmatically check if user has selected High Accuracy mode if not then enable it automatically. Is it possible ? Please advice.

推荐答案

API 级别 19 (Kitkat) 起,可以获取设备的当前位置模式:

It is possible to get the device's current location mode since API level 19 (Kitkat):

public int getLocationMode(Context context) {
    return Settings.Secure.getInt(activityUnderTest.getContentResolver(), Settings.Secure.LOCATION_MODE);
}

这些是可能的返回值(请参阅此处):

These are the possible return values (see here):

0 = LOCATION_MODE_OFF  
1 = LOCATION_MODE_SENSORS_ONLY  
2 = LOCATION_MODE_BATTERY_SAVING  
3 = LOCATION_MODE_HIGH_ACCURACY

所以你想要类似的东西

if(getLocationMode(context) == 3) {
    // do stuff
}

很遗憾,您无法以编程方式设置位置模式,但您可以将用户直接发送到设置屏幕,他可以在那里执行此操作:

Unfortunately you can't set the location mode programmatically but you can send the user directly to the settings screen where he can do that:

startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));

这篇关于以编程方式将定位模式更改为高精度 Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆