【发布时间】:2017-12-29 04:48:53
【问题描述】:
如果位置在 android 中被用户关闭,我如何识别? 我还想显示一个对话框以转到设置。
【问题讨论】:
如果位置在 android 中被用户关闭,我如何识别? 我还想显示一个对话框以转到设置。
【问题讨论】:
public static boolean canGetLocation() {
return isLocationEnabled(App.appInstance); // application context
}
public static boolean isLocationEnabled(Context context) {
int locationMode = 0;
String locationProviders;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
try {
locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
return locationMode != Settings.Secure.LOCATION_MODE_OFF;
} else {
locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
return !TextUtils.isEmpty(locationProviders);
}
}
这是一个快速检查设备位置是否启用的工具,希望对您有所帮助。
这将返回一个布尔值,指示是否启用。
您可以使用警报对话框单击侦听器中的以下意图来导航而不是位置设置,
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
【讨论】:
您可以触发广播接收器以了解 gps 已打开/关闭。
答案在这里:How to trigger broadcast receiver when gps is turn on/off?
检查gps是否开启:How to check if GPS is Disabled Android
【讨论】: