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

后端 未结 5 723
无人共我
无人共我 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 14:01

    You can check it like that:

    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) // Return a boolean
    

    EDIT:

    If you want to check the network provider:

    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) // Return a boolean
    

    EDIT 2:

    If you want to open the settings, you can use this intent:

    Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    

提交回复
热议问题