Italian [SOLVED] get phone language

sirjo66

Well-Known Member
Licensed User
Longtime User
Hello,
I need to get phone language.
Searching in internet, I found this Java code:
B4X:
Locale locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    locale = Resources.getSystem().getConfiguration().getLocales().get(0);
} else {
    //noinspection deprecation
    locale = Resources.getSystem().getConfiguration().locale;
}

Well, the line
B4X:
locale = Resources.getSystem().getConfiguration().getLocales().get(0);
no problem, here is the code:
B4X:
Dim Resources As JavaObject
Resources = Resources.InitializeStatic("android.content.res.Resources").RunMethodJO("getSystem", Null).RunMethodJO("getConfiguration", Null).RunMethodJO("getLocales", Null)
Dim locale As String = Resources.RunMethod("get", Array(0))
and it works very well with API 26 and later, but the line (for API < 26)
B4X:
locale = Resources.getSystem().getConfiguration().locale;
I don't know how to do it

I tried
B4X:
Dim Resources As JavaObject
Resources = Resources.InitializeStatic("android.content.res.Resources").RunMethodJO("getSystem", Null).RunMethod("getConfiguration", Null)
' until here it works, now I don't know how get "locale"
Dim str As String = Resources
Log(str) ' this works, it show to me "Resources" variable, I see inside it "en_GB", but I don't how to extract it correctly

Dim locale As String = Resources.RunMethod("locale", Null) ' <<<<<<< ERROR
Log(locale)
The ERROR is: java.lang.RuntimeException: Method: locale not found in: android.content.res.Configuration

I try with
B4X:
Dim locale As List
but don't works, I think that the problem is RunMethod

Thanks
Sergio
 

sirjo66

Well-Known Member
Licensed User
Longtime User
I'm sorry, my mistake
I solved with:
B4X:
    Dim Resources As JavaObject
    Resources = Resources.InitializeStatic("android.content.res.Resources").RunMethodJO("getSystem", Null).RunMethod("getConfiguration", Null)
    Dim locale As String = Resources.GetField("locale")
 

Star-Dust

Expert
Licensed User
Longtime User
Io uso questo di solito:
B4X:
Sub Lingua As String
Dim r As Reflector
r.Target = r.RunStaticMethod("java.util.Locale", "getDefault", Null, Null)
Return r.RunMethod("getLanguage")
End Sub
 

sirjo66

Well-Known Member
Licensed User
Longtime User
😂 ho sbagliato sezione, ero convinto di averlo scritto nel forum inglese
 
Top