用于GPS的IsProviderEnabled不起作用

时间:2012-07-05 09:46:26

标签: android gps

我们已经为Android实施了基于地理位置的应用,因此我们需要确保始终启用GPS。问题是

manager.isProviderEnabled( LocationManager.GPS_PROVIDER )
即使启用了GPS提供商,

也始终返回false,因此我们的应用程序始终显示警报以更改GPS状态或无法正常工作。

你知道发生了什么吗?

我们正在使用三星Galaxy S和HTC Wildfire设备进行测试...... 提前谢谢。

3 个答案:

答案 0 :(得分:3)

您可以直接从系统获取GPS状态:

LocationManager myLocationManager = (LocationManager)  getSystemService(Context.LOCATION_SERVICE);

private boolean getGPSStatus()
{
   String allowedLocationProviders =
   Settings.System.getString(getContentResolver(),
   Settings.System.LOCATION_PROVIDERS_ALLOWED);

   if (allowedLocationProviders == null) {
      allowedLocationProviders = "";
   }

   return allowedLocationProviders.contains(LocationManager.GPS_PROVIDER);
} 

答案 1 :(得分:0)

您需要先检查手机上是否确实存在GPS。 如果您的手机很便宜,那么它最有可能将网络位置用作位置提供商。

你可以试试这个:

private boolean isGPSEnabled() {
Context context = Session.getInstance().getCurrentPresenter().getViewContext();

LocationManager locationMgr = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

boolean GPS_Sts = locationMgr.isProviderEnabled(LocationManager.NETWORK_PROVIDER)|| locationMgr.isProviderEnabled(LocationManager.GPS_PROVIDER);

return GPS_Sts;
}

答案 2 :(得分:0)

Sometimes your device settings is set to get the location using WiFi Networks not GPS system, so that the location would be opened but your app will return false when checking for the GPS_PROVIDER.

The proper solution to that is to check for both, GPS & Network:

if you want to check using Settings:

private boolean checkIfLocationOpened() {
    String provider = Settings.Secure.getString(getActivity().getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
    if (provider.contains("gps") || provider.contains("network"))
        return true;
    }
    // otherwise return false
    return false;
}

and if you want to do it using the LocationManager:

private boolean checkIfLocationOpened() {
    final LocationManager manager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
    if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER) || manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
        return true;
    }
    // otherwise return false
    return false;
}

You can find the complete details at my answer here.