Settings.Secure-不建议使用LOCATION_MODE-替代编写

时间:2019-03-13 10:55:44

标签: android

根据this,标志Settings.Secure.LOCATION_MODE在API 28中已被弃用。是否有另一种方法写入? (我对读取此值不感兴趣,可以通过LocationManager完成此操作)

或者,我是否可以依靠以下内容在所有设备上仍然有效? (例如,它可以在我的android Pie设备上运行):

Settings.Secure.putInt(contentResolver, Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF)
Settings.Secure.putInt(contentResolver, Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_HIGH_ACCURACY)

我只是想知道在某些设备上上述操作是否可能失败,或者即使在API 28或更高版本上,我是否也可以节省下来继续使用它们...

1 个答案:

答案 0 :(得分:-1)

我使用此方法检查是否启用了定位服务

   public static Boolean isLocationEnabled(Context context) {        
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            // This is new method provided in API 28
            LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
            return lm.isLocationEnabled();
        } else {
            // This is Deprecated in API 28
            int mode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE,
                    Settings.Secure.LOCATION_MODE_OFF);
            return (mode != Settings.Secure.LOCATION_MODE_OFF);
        }
    }