GPS isProviderEnabled always return false

后端 未结 6 1203
梦谈多话
梦谈多话 2021-01-11 11:16

I\'ve got this code

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


        
6条回答
  •  被撕碎了的回忆
    2021-01-11 12:02

    some time location manager returns the wrong result.below code works for me.

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

提交回复
热议问题