Android:检查是否启用了google设置位置

Android:检查是否启用了google设置位置,android,location,google-play-services,Android,Location,Google Play Services,我正在开发一款使用google play服务的应用程序。在某些手机上,客户端返回null表示位置。发生这种情况的原因是,在谷歌设置上没有启用locaiton选项,如所附图片中所示 如何通过编程检查android应用程序中是否启用了google设置位置 操作位置源设置向用户显示允许配置当前位置源的设置。可以检查google play服务本身是否可用。请按照此处的说明操作: 即使启用了Google Play Services位置服务,getLastLocation()也可能返回null。例如,在

我正在开发一款使用google play服务的应用程序。在某些手机上,客户端返回null表示位置。发生这种情况的原因是,在谷歌设置上没有启用locaiton选项,如所附图片中所示

如何通过编程检查android应用程序中是否启用了google设置位置


操作位置源设置向用户显示允许配置当前位置源的设置。

可以检查google play服务本身是否可用。请按照此处的说明操作:

即使启用了Google Play Services位置服务,getLastLocation()也可能返回null。例如,在重新启用它们之后。但是,您可以使用以下意图将用户带到Google Play Services位置设置:

Intent settings = new Intent("com.google.android.gms.location.settings.GOOGLE_LOCATION_SETTINGS");
startActivity(settings);
最后,还可以检查是否启用了Android定位服务:

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 locationMode != Settings.Secure.LOCATION_MODE_OFF;

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


} 

应该有一个更改,您可以在其中选中
TextUtils.isEmpty(locationProviders)
。它从来都不是空的。在没有提供程序(位置设置被禁用)的情况下,它具有值
“null”
。是的,我的意思是value
“null”
,而不是
null
,我认为这是最好的解决方案

您可以通过以下代码进行检查:

    /**
     * This is used to check whether location is enabled or not.
     * @param mContext
     * @return
     */
    public static boolean isDeviceLocationEnabled(Context mContext) {
        int locMode = 0;
        String locProviders;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
            try {
                locMode = Settings.Secure.getInt(mContext.getContentResolver(), Settings.Secure.LOCATION_MODE);
            } catch (Settings.SettingNotFoundException e) {
                e.printStackTrace();
                return false;
            }
            return locMode != Settings.Secure.LOCATION_MODE_OFF;
        }else{
            locProviders = Settings.Secure.getString(mContext.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
            return !TextUtils.isEmpty(locProviders);
        }
    }

使用
LocationManager
检查不同的提供程序为什么首先将
lm
的值设置为
null
,然后检查它是否等于
null
,如果等于,则将其值设置为系统位置服务,此时您可以在一行上声明它,如下所示:
LocationManager lm=(LocationManager)getSystemService(context.LOCATION\u服务)嗨!这个解决方案真的很好。只有一件事,Settings.Secure.LOCATION\u PROVIDERS\u ALLOWED在API级别17中被弃用,Kitkat的API级别为19。在if-else块中缺少API级别18。我说得对吗?很抱歉延迟回复。不确定,但我在2014年写了这篇文章,所以从那以后事情肯定发生了变化。
    /**
     * This is used to check whether location is enabled or not.
     * @param mContext
     * @return
     */
    public static boolean isDeviceLocationEnabled(Context mContext) {
        int locMode = 0;
        String locProviders;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
            try {
                locMode = Settings.Secure.getInt(mContext.getContentResolver(), Settings.Secure.LOCATION_MODE);
            } catch (Settings.SettingNotFoundException e) {
                e.printStackTrace();
                return false;
            }
            return locMode != Settings.Secure.LOCATION_MODE_OFF;
        }else{
            locProviders = Settings.Secure.getString(mContext.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
            return !TextUtils.isEmpty(locProviders);
        }
    }