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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25175517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:10:46  来源:igfitidea点击:

Change Location Mode to High Accuracy Programmatically Android

androidgpslocationandroid-locationandroid-geofence

提问by ik024

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.Hight Accuracy

1.高精度

2.Battery Saving

2.省电

3.GPS Only

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.

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

回答by kaolick

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

API 级别 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

So you want something like

所以你想要类似的东西

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

回答by userM1433372

You can provide criteria in the LocationMamager.requestLocationUpdates. As criteria you can provide one of the following values to select the accuracy needed.

您可以在 LocationMamager.requestLocationUpdates 中提供条件。作为标准,您可以提供以下值之一来选择所需的精度。

Constants
int ACCURACY_COARSE A constant indicating an approximate accuracy requirement
int ACCURACY_FINE   A constant indicating a finer location accuracy requirement
int ACCURACY_HIGH   a constant indicating a high accuracy requirement - may be used for horizontal, altitude, speed or bearing accuracy.
int ACCURACY_LOW    A constant indicating a low location accuracy requirement - may be used for horizontal, altitude, speed or bearing accuracy.
int ACCURACY_MEDIUM A constant indicating a medium accuracy requirement - currently used only for horizontal accuracy.

See http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates(long, float, android.location.Criteria, android.app.PendingIntent)

http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates(long, float, android.location.Criteria, android.app.PendingIntent)

回答by Magnus W

Since API level 28, you should use LocationManager.isProviderEnabled()to find out if a specific provider is enabled or not.

从 API 级别 28 开始,您应该使用LocationManager.isProviderEnabled()来确定是否启用了特定的提供程序。

Changing the setting programmatically from your app is unfortunately not possible.

遗憾的是,无法通过您的应用程序以编程方式更改设置。