java如何在我的应用程序中启用网络和gps定位?
我正在尝试获取用户的位置:
//获取GPS状态
isGPSEnabled = locationManage.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
Log.e(MyLogger.TAG, "Non providers are enabled");
我需要用户设置enable network location
或turn on GPS
我如何帮助用户完成这些选项
open the device settings page and return to my activity on back press.
change these two device settings from within my application only?
这意味着打开一个确认对话框来启用这两个设置
# 1 楼答案
使用最新的位置api,您可以通过应用程序内的设置对话框提示用户动态更改位置设置
https://developer.android.com/training/location/change-location-settings.html
# 2 楼答案
startActivityForResult(新意图(android.provider.Settings.ACTION\u LOCATION\u SOURCE\u Settings),0)
# 3 楼答案
在4.1之后,在代码中更改这些设置是不可能的(它们可能是打开它的一个变通方法,因为有一个bug see here),但在4.3或4.4上不起作用,或者你需要根手机,你可以做的是向用户显示一个对话框,以转到设置以启用GPS
调用中的上一个函数
然后在
onActivityResult
中,您可以检查代码0是否已打开GPS