Check if 'Access to my location' is enabled - Android

后端 未结 5 808
无人共我
无人共我 2021-02-02 13:33

I have an android app that uses location. But I noticed that if users disable the \'Access to my location\' in Settings > Location access, nothing works anymore. How can I check

5条回答
  •  眼角桃花
    2021-02-02 13:57

    An alternative way to do that without using Settings.Secure and without scanning all location providers would be to do:

    LocationManager locationManager = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
    int providersCount = locationManager.getProviders(true).size(); // Listing enabled providers only
    if (providersCount == 0) {
        // No location providers at all, location is off
    } 
    

提交回复
热议问题