Android: 检查谷歌设置位置是否已启用

6

使用 LocationManager 来检查不同的提供者。 - tyczj
可以检查Google Play服务是否可用。请按照以下说明操作:https://developer.android.com/training/location/retrieve-current.html 即使启用了Google Play服务位置服务,getLastLocation()也可能返回null。例如,在重新启用它们之后立即发生这种情况。但是,您可以使用此Intent将用户带到Google Play服务位置设置。链接 - anon
2个回答

1
我认为这是最好的解决方案。
您可以通过以下代码进行检查:
    /**
     * 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);
        }
    }

0

应该有一个更改,您需要检查TextUtils.isEmpty(locationProviders)。它永远不为空。当没有提供程序(位置设置已禁用)时,它的值为"null"。是的,我指的是值"null",而不是null


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接