如何在Android Pre Nougat<API 24上获取设备语言环境?

问题描述

我需要在Android Studio上获得设备Locale,但显然以下代码仅适用于api级24+;我如何在较低级别的api中获得与这行代码相同的结果?

getResources().getConfiguration().getLocales().get(0);

解决方法

根据Google文档,该命令public Locale locale已在API 24中弃用,并且您可以在24级以上的API级别中使用getLocales()setLocales(android.os.LocaleList)。因此,您应该{{1 }},而不是较低的API。

configuration.locale

还要检查一下,说明一切:

Set Locale programmatically

,

对于下层Lavel API,可以使用

Locale current = getResources().getConfiguration().locale;

参考:https://stackoverflow.com/a/14389640/12676247