检查是否启用了"访问我的位置" - Android

Rom*_*rin 18 gps android location

我有一个使用位置的Android应用程序.但我注意到,如果用户在"设置">"位置"访问权限中禁用"访问我的位置",则无处可用.如何检查它是否已启用?如果它被禁用,如何从我的应用程序打开这些设置?

谢谢

解决了 :

String locationProviders = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (locationProviders == null || locationProviders.equals("")) {
    ...
    startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
Run Code Online (Sandbox Code Playgroud)

Brt*_*tle 8

您可以这样检查:

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) // Return a boolean
Run Code Online (Sandbox Code Playgroud)

编辑:

如果要检查网络提供商:

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) // Return a boolean
Run Code Online (Sandbox Code Playgroud)

编辑2:

如果要打开设置,可以使用此意图:

Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
Run Code Online (Sandbox Code Playgroud)


Pra*_*har 3

可能这会很有用,检查这个网站,它讨论了位置服务

http://www.scotthelme.co.uk/android-location-services/