GPS isProviderEnabled总是返回false

时间:2014-02-06 10:21:46

标签: android gps return provider

我有这段代码

 lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
 boolean isGPS = lm.isProviderEnabled (LocationManager.GPS_PROVIDER);

即使启用了GPS,也始终返回false。 GPS正常工作,但我使用此布尔值显示弹出“没有启用GPS”。在这种情况下,每次都会出现弹出窗口

  1. 我检查了类似的问题,但这对我没有帮助。
  2. 是的,我有权使用我的清单
  3. 我在onResume方法中使用此代码
  4. 感谢您的帮助。

6 个答案:

答案 0 :(得分:20)

If you are checking for the location is opened or not whatever it is using GPS or not, so you have to pay attention to the following as it was my case, in device location settings the locating method was set to BatterySaving mode, in which the device uses only WiFi mobile networks to estimate location:

enter image description here

Therefore, the GPS is not even used in update location, and so the location icon will not appear in the status bar in addition to the location provider will be stated as Network not GPS.

so, to solve that issue you have to check whether the provider contains gps or contains network:

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

答案 1 :(得分:8)

试试这个..

private void turnGPSOn(){   
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);   
if(!provider.contains("gps")){      
    final Intent poke = new Intent();  
    poke.setClassName("com.android.settings","com.android.settings.widget.SettingsAppWidgetProvider");           
    poke.addCategory(Intent.CATEGORY_ALTERNATIVE);   
    poke.setData(Uri.parse("3"));      
    sendBroadcast(poke);  
    } 
}  

答案 2 :(得分:2)

某些时候,位置管理器返回错误的结果。下面的代码对我有用。

public static boolean isGpsEnabled(Context context) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            String providers = Settings.Secure.getString(context.getContentResolver(),
                    Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
            if (TextUtils.isEmpty(providers)) {
                return false;
            }
            return providers.contains(LocationManager.GPS_PROVIDER);
        } else {
            final int locationMode;
            try {
                locationMode = Settings.Secure.getInt(context.getContentResolver(),
                        Settings.Secure.LOCATION_MODE);
            } catch (Settings.SettingNotFoundException e) {
                e.printStackTrace();
                return false;
            }
            switch (locationMode) {

                case Settings.Secure.LOCATION_MODE_HIGH_ACCURACY:
                case Settings.Secure.LOCATION_MODE_SENSORS_ONLY:
                    return true;
                case Settings.Secure.LOCATION_MODE_BATTERY_SAVING:
                case Settings.Secure.LOCATION_MODE_OFF:
                default:
                    return false;
            }
        }
    }

答案 3 :(得分:0)

试试此GPS示例代码。希望它对你有所帮助。

GPS Location

答案 4 :(得分:0)

有时你必须重新启动你的设备,那时gps-status返回失败。

答案 5 :(得分:0)

修复GPS isProviderEnabled在Android 6上始终返回false,将其添加到清单中:

<uses-feature android:name="android.hardware.location.gps" />

Titanium在Android 5.0及以上版本找不到GPS硬件以上,因此它不断返回虚假。添加以上行以解决问题。