以编程方式将位置模式更改为高精度 Android位置、模式、方式、Android

2023-09-07 04:28:45 作者:良人痞性

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

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 level 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));