如果用户关闭位置,如何在android中知道

Sob*_*ott 0 android location

如果用户在android中关闭了位置,我如何识别?我还想显示一个对话框以转到设置。

San*_*oop 6

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

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