How to get the device locale after changing the application locale

时光毁灭记忆、已成空白 提交于 2019-11-30 13:05:28

I am absolutely unsure how portable this is to different devices:

try {
    Process exec = Runtime.getRuntime().exec(new String[]{"getprop", "persist.sys.language"});
    String locale = new BufferedReader(new InputStreamReader(exec.getInputStream())).readLine();
    exec.destroy();
    Log.e("", "Device locale: "+locale);
} catch (IOException e) {
    e.printStackTrace();
}

And if you want the country part: persist.sys.country

I asked something similar and found this answer, sorry if it is late:

To find the system locale use:

defaultLocale = Resources.getSystem().getConfiguration().locale;

It gets the system locale, no matter which default locale is set for the app/activity.

for those whom are using Daniel Fekete 's solution which is :

Process exec = Runtime.getRuntime().exec(new String[]{"getprop", "persist.sys.language"});
String locale = new BufferedReader(new InputStreamReader(exec.getInputStream())).readLine();
exec.destroy();

Be aware of that locale might be EMPTY on latest Android Api(+21)! instead of using persist.sys.language use persist.sys.locale

exec = Runtime.getRuntime().exec(new String[]{"getprop", "persist.sys.locale"});
locale = new BufferedReader(new InputStreamReader(exec.getInputStream())).readLine();
exec.destroy();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!