如何在Android中知道用户是否关闭了位置 [英] How to know in android if location is turned off by user

查看:134
本文介绍了如何在Android中知道用户是否关闭了位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I identify that if the location turned off by the user in android? I also want to show a dialog to go to settings.

推荐答案

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

这篇关于如何在Android中知道用户是否关闭了位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆