【问题标题】:Change Location Mode to High Accuracy Programmatically Android以编程方式将位置模式更改为高精度 Android
【发布时间】:2014-09-30 06:45:45
【问题描述】:

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

1.高精度

2.省电

3.仅限GPS

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

【问题讨论】:

标签: android gps location android-location android-geofence


【解决方案1】:

可以从API level 19 (Kitkat)获取设备当前的定位模式:

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

这些是可能的返回值(参见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
}

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

startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));

【讨论】:

  • 您确定不能以编程方式更改位置模式吗?我正在尝试做类似的事情,如果可以的话,我想自动更改它们。因此,如果您确定这是不可能的,那就太好了。
  • @TBear 是的,非常舒尔。我认为不应允许应用程序操纵这种全局设置。
  • LOCATION_MODE_... 现已弃用。
【解决方案2】:

您可以在 LocationMaager.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.

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

【讨论】:

  • 在 android API 31 中不推荐使用提供条件
【解决方案3】:

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

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

【讨论】:

  • 这与GPS是否开启无关;例如,它已打开,但已设置为省电模式。
猜你喜欢
  • 2014-11-21
  • 1970-01-01
  • 2015-09-30
  • 2023-01-17
  • 2014-01-22
  • 2017-04-25
  • 2014-07-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多