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

如何在android中知道位置是否被用户关闭,android,location,Android,Location,如果用户在android中关闭了位置,我如何识别? 我还想显示一个对话框以转到“设置” public static boolean canGetLocation() { return isLocationEnabled(App.appInstance); // application context } public static boolean isLocationEnabled(Context context) { int locationMode = 0; Str

如果用户在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));
这是一个检查设备位置是否启用的快速工具,希望能有所帮助

这将返回一个布尔值,指示是否启用

您可以通过使用“警报”对话框中的以下意图来导航“不定位”设置,单击“侦听器”

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

您可以触发广播接收器以知道gps已打开/关闭

答案如下:


要检查gps是否打开或关闭:

您可以触发广播接收器,以知道gps已打开/关闭

答案如下:


要检查gps是否打开或关闭:

谢谢!工作起来很有魅力!Settings.Secure.LOCATION\u模式现在已不推荐使用。有其他解决方案吗?@nandu您针对哪个Sdk版本?@Sanoop version 28谢谢!工作起来很有魅力!Settings.Secure.LOCATION\u模式现在已不推荐使用。有其他解决方案吗?@nandu您的目标Sdk版本是哪个?@Sanoop版本28