且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Android中知道用户是否关闭了位置

更新时间:2023-11-29 09:55:58

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

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

This is a quick tool to check if device location is enabled or not, Hope this helps.

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

This will return a boolean indicating enabled or not.

并且您可以通过在警报对话框的点击侦听器中使用以下意图来导航非位置设置

And you can navigate not location settings by using the following intent from the alert dialog click listener,

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