【问题标题】:How to know in android if location is turned off by user [duplicate]如何在android中知道用户是否关闭了位置[重复]
【发布时间】:2017-12-29 04:48:53
【问题描述】:

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

【问题讨论】:

    标签: android location


    【解决方案1】:
    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));
    

    【讨论】:

    • 谢谢!像魅力一样工作!
    • Settings.Secure.LOCATION_MODE 现在已弃用。任何替代解决方案?
    • @nandu 你的目标是哪个 SDK 版本?
    • @Sanoop 版本 28
    【解决方案2】:

    您可以触发广播接收器以了解 gps 已打开/关闭。

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

    检查gps是否开启:How to check if GPS is Disabled Android

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-09
      • 1970-01-01
      • 1970-01-01
      • 2019-09-06
      • 2023-04-11
      • 1970-01-01
      • 1970-01-01
      • 2013-09-03
      相关资源
      最近更新 更多