I am trying to return the locale in my app with this line of code:
Locale current = getResources().getConfiguration().locale;
The minSdkVersion is 15 and compiling with 24. But locale is deprecated, does it affect my app efficiency?
Is there any other 'not deprecated' way to retrieve the locale?
If you are compiling with API 24 or above you should do this. Still it will show deprecated for lower one but you can ignore that.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
locale = getResources().getConfiguration().getLocales().get(0);
} else {
locale = getResources().getConfiguration().locale;
}
Related
I am developing an Android application that can set up two languages.
And I would like to divide the default language and the setting language by distinguishing between the language set on the phone and the language set inside the app.
However, if you implement a language change function, the default language settings that can be known through googling will be returned within the app.
During the search process, I learned that individual languages for each app can be set from Android 13.
Is there any way to check Android's default language and app setting language in Android 12 or lower versions?
Below are the functions I used. I'd appreciate it if you could answer me.
'''
Log.e("contry","resources.configuration.locales.get(0) = ${resources.configuration.locales.get(0)}")
Log.e("contry","resources.configuration.locales.get(0).language = ${resources.configuration.locales.get(0).language}")
Log.e("contry","Locale.getDefault().language = ${Locale.getDefault().language}")
Log.e("contry","getSystemLanguage = ${getSystemLanguage(this)}")
Log.e("contry","LocaleList.getDefault = ${LocaleList.getDefault()}")
fun getSystemLanguage(context: Context): String {
val systemLocale: Locale
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
systemLocale = context.getResources().getConfiguration().getLocales().get(0)
} else {
systemLocale = context.getResources().getConfiguration().locale
}
return systemLocale.language // ko
}
'''
From android 11 setUserAuthenticationValidityDurationSeconds are deprecated in favor of setUserAuthenticationParameters inside KeyGenParameterSpec.Builder but seams there is any support for previous versions.
so, what are best the solution ?
KeyGenParameterSpec.Builder builder = new KeyGenParameterSpec.Builder(...)
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R)
builder.setUserAuthenticationParameters(timeout, KeyProperties.AUTH_DEVICE_CREDENTIAL | KeyProperties.AUTH_BIOMETRIC_STRONG);
else
//noinspection deprecation
builder.setUserAuthenticationValidityDurationSeconds(timeout);
this one?
You don't need to migrate the actual keys, when you are ready to support Android 11 you can just switch to something like this, (don't forget to set compileSdkVersion 30 for the new APIs)
val timeout = 30 //seconds
val builder = KeyGenParameterSpec.Builder(...)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
builder.setUserAuthenticationParameters(timeout,
KeyProperties.AUTH_DEVICE_CREDENTIAL
or KeyProperties.AUTH_BIOMETRIC_STRONG
)
} else {
builder.setUserAuthenticationValidityDurationSeconds(timeout)
}
You can do this because internally setUserAuthenticationValidityDurationSeconds is doing the same thing. The only exception is if you are calling setUserAuthenticationValidityDurationSeconds with -1 as the timeout. In this case the equivalent with the new API is builder.setUserAuthenticationParameters(0, KeyProperties.AUTH_BIOMETRIC_STRONG).
You can check here the source code for both cases.
PS: The code above is minAPI 24, you need to wrap the code above in more build checks if you are at a lower API level.
I need to get the device Locale on android studio, but apparently the code below only works on api level 24+; How can i achieve the same result as this line of code in lower level apis?
getResources().getConfiguration().getLocales().get(0);
well according to google docs the command public Locale locale has deprecated in API 24 and you are allowed to use getLocales() and setLocales(android.os.LocaleList) in API levels above 24. so you should yoconfiguration.locale instead in lower APIs.
#Override
public void applyOverrideConfiguration(Configuration overrideConfiguration) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && Build.VERSION.SDK_INT <= Build.VERSION_CODES.N_MR1) {
// update overrideConfiguration with your locale
setLocale(overrideConfiguration) // you need to implement this
}
super.applyOverrideConfiguration(overrideConfiguration);
}
also check this out, it says it all:
Set Locale programmatically
For lower lavel api, you can use
Locale current = getResources().getConfiguration().locale;
Reference: https://stackoverflow.com/a/14389640/12676247
I am developing an app which is to be opened directly in Portuguese language and inside that app it has having the option of changing the language to English. So i have to change the language in the code . But when i tested my app in Lollipop, it is directly opening in English Language.
In all other versions except the lollipop , it is working fine.
Can some one help me or suggest me the steps which i have to take to solve this bug.
Thanks
Here is the code which i am using for the localization.
Have some methods been "depreciated" from lollipop?
public static Locale locale = new Locale("pt_BR");
Locale.setDefault(locale);
android.content.res.Configuration config = new android.content.res.Configuration();
config.locale = locale;
this.getResources().updateConfiguration(config, null);
String locale = this.getResources().getConfiguration().locale
.getDisplayName();
Log.i("System out", "(LogIn)Current Language : " + locale);
Due to some security issue --> java.lang.SecurityException: Permission Denial:
Language changing is not working, please try to change
Locale locale = Locale("en_US");
to
Locale locale = Locale("en", "US"); //Locale locale = Locale("language", "Country")
I've found using the android.text.format.DateUtils relative APIs that return values like "yesterday" or "2 hours ago" very nice - but my app does not support every language Android does. So, I default to English, but for every language I don't support, the relative string shows in the device's setting.
For example, like:
Last attempt: hace 11 minutos.
I'd like to make the API call default to English for any languages I don't support. However, I don't see anywhere to set the Locale for the API call - I'm hoping I'm just missing it somewhere.
Is there a way to set the Locale just for the API call, ignoring the device setting?
This is working for me up to Android 7
void forceLocale(Locale locale) {
Configuration conf = getBaseContext().getResources().getConfiguration();
updateConfiguration(conf, locale);
getBaseContext().getResources().updateConfiguration(conf, getResources().getDisplayMetrics());
Configuration systemConf = Resources.getSystem().getConfiguration();
updateConfiguration(systemConf, locale);
Resources.getSystem().updateConfiguration(conf, getResources().getDisplayMetrics());
Locale.setDefault(locale);
}
void updateConfiguration(Configuration conf, Locale locale) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
conf.setLocale(locale);
}else {
//noinspection deprecation
conf.locale = locale;
}
}
According to the source code of the DateUtils class it uses both Resource.getSystem() and Locale.getDefault() method for formatting date and time. You can change the default Locale using Locale.setDefault() method but I don't think it's possible to change the return value of the Resource.getSystem() method. You can try to change the default locale to Locale.US but it seems to me that results will be even worse in this case.