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

后端 未结 5 714
无人共我
无人共我 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

    Unfortunately, it seems the using of Settings.Secure.LOCATION_PROVIDERS_ALLOWED is deprecated since API 19.

    A new way to do that would be:

    int locationMode = Settings.Secure.getInt(
        getContentResolver(),
        Settings.Secure.LOCATION_MODE,
        Settings.Secure.LOCATION_MODE_OFF // Default value if not found
    );
    
    if (locationMode == Settings.Secure.LOCATION_MODE_OFF) {
        // Location is off
    }
    

提交回复
热议问题