Commit fedd3d93 by zhengnw@sobot.com

widget 1.0.8 baseactivity 修改国际化语言

parent 5b5ea793
......@@ -13,7 +13,7 @@ ext {
PUBLISH_GROUP_ID = "com.sobot.library" //项目包名
PUBLISH_ARTIFACT_ID = 'widget' //项目名
// PUBLISH_ARTIFACT_ID = 'widget_x' //项目名
PUBLISH_VERSION = '1.0.7' //版本号
PUBLISH_VERSION = '1.0.8' //版本号
}
......
......@@ -11,6 +11,7 @@ import android.graphics.drawable.Drawable;
import android.hardware.Camera;
import android.os.Build;
import android.os.Bundle;
import android.os.LocaleList;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
......@@ -59,6 +60,8 @@ public abstract class SobotBaseActivity extends FragmentActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
//修改国际化语言
changeAppLanguage();
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT != Build.VERSION_CODES.O) {
if (!SobotWidgetApi.getSwitchMarkStatus(SobotMarkConfig.LANDSCAPE_SCREEN)) {
......@@ -99,10 +102,6 @@ public abstract class SobotBaseActivity extends FragmentActivity {
} catch (Exception e) {
e.printStackTrace();
}
//修改国际化语言
changeAppLanguage();
//左上角返回按钮水滴屏适配
if (getLeftMenu() != null) {
displayInNotch(getLeftMenu());
......@@ -138,18 +137,77 @@ public abstract class SobotBaseActivity extends FragmentActivity {
}
}
/**
* 获取系统首选语言
* 注意:该方法获取的是用户实际设置的不经API调整的系统首选语言
*/
public static Locale getSysPreferredLocale() {
Locale locale;
//7.0以下直接获取系统默认语言
if (Build.VERSION.SDK_INT < 24) {
// 等同于context.getResources().getConfiguration().locale;
locale = Locale.getDefault();
// 7.0以上获取系统首选语言
} else {
/*
* 以下两种方法等价,都是获取经API调整过的系统语言列表(可能与用户实际设置的不同)
* 1.context.getResources().getConfiguration().getLocales()
* 2.LocaleList.getAdjustedDefault()
*/
// 获取用户实际设置的语言列表
locale = LocaleList.getDefault().get(0);
}
return locale;
}
//使用系统语言
public void useSysLanguage() {
try {
Locale locale = getSysPreferredLocale();
updateLanguage(locale);
} catch (Exception e) {
e.printStackTrace();
}
}
public void changeAppLanguage() {
Locale language = (Locale) SobotSharedPreferencesUtil.getInstance(getSobotBaseContext()).get("SobotLanguage", Locale.class);
if (language != null) {
// 本地语言设置
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = new Configuration();
conf.locale = language;
res.updateConfiguration(conf, dm);
try {
Locale locale = (Locale) SobotSharedPreferencesUtil.getInstance(getSobotBaseContext()).get("SobotLanguage", Locale.class);
updateLanguage(locale);
} catch (Exception e) {
e.printStackTrace();
}
}
public void updateLanguage(Locale locale) {
try {
if (locale != null) {
Resources resources = getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
Configuration configuration = resources.getConfiguration();
//Android 7.0以上的方法
if (Build.VERSION.SDK_INT >= 24) {
configuration.setLocale(locale);
configuration.setLocales(new LocaleList(locale));
createConfigurationContext(configuration);
//实测,updateConfiguration这个方法虽然很多博主说是版本不适用
//但是我的生产环境androidX+Android Q环境下,必须加上这一句,才可以通过重启App来切换语言
resources.updateConfiguration(configuration, metrics);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
//Android 4.1 以上方法
configuration.setLocale(locale);
resources.updateConfiguration(configuration, metrics);
} else {
configuration.locale = locale;
resources.updateConfiguration(configuration, metrics);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
protected void setUpToolBarRightMenu() {
if (getRightMenu() != null) {
......@@ -817,7 +875,7 @@ public abstract class SobotBaseActivity extends FragmentActivity {
}
}
};
if (checkIsShowPermissionPop(TextUtils.isEmpty(title) ? getResources().getString(R.string.sobot_camera) : title, TextUtils.isEmpty(content) ? getResources().getString(R.string.sobot_camera_yongtu) : content, 3,4)) {
if (checkIsShowPermissionPop(TextUtils.isEmpty(title) ? getResources().getString(R.string.sobot_camera) : title, TextUtils.isEmpty(content) ? getResources().getString(R.string.sobot_camera_yongtu) : content, 3, 4)) {
return;
}
if (!checkCameraPermission()) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment