如何在 android 中知道位置是否被用户关闭

How to know in android if location is turned off by user

如果 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);
    }
}

这是一个快速检查设备位置是否启用的工具,希望对您有所帮助。

这将 return 一个指示启用与否的布尔值。

并且您可以使用警报对话框点击侦听器中的以下意图导航而不是位置设置,

startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));

您可以触发广播接收器以了解 gps 是否开启。

答案在这里:How to trigger broadcast receiver when gps is turn on/off?

检查 gps 是否打开或关闭: