如何检查是否在Android 4.4及更高版本中以编程方式打开或关闭了GPS? [英] How to check if gps i turned on or not programmatically in android version 4.4 and above?

查看:110
本文介绍了如何检查是否在Android 4.4及更高版本中以编程方式打开或关闭了GPS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在线搜索答案,但它们似乎仅适用于Android 4.0及以下版本

I have tried searching for answers online but they seemed to work only for android versions 4.0 and below

推荐答案

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 (SettingNotFoundException e) {
            e.printStackTrace();
            return false;
        }

        return locationMode != Settings.Secure.LOCATION_MODE_OFF;

    }else{
        locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
        return !TextUtils.isEmpty(locationProviders);
    }


} 

这篇关于如何检查是否在Android 4.4及更高版本中以编程方式打开或关闭了GPS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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