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

Change Location Mode to High Accuracy Programmatically Android(以编程方式将位置模式更改为高精度 Android)
本文介绍了以编程方式将位置模式更改为高精度 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 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));

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)