GPS isProviderEnabled always return false

后端 未结 6 1246
梦谈多话
梦谈多话 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:19

    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:

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

提交回复
热议问题