diff --git a/app/build.gradle b/app/build.gradle index b002d6f..e1a1233 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,12 +1,12 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 26 + compileSdkVersion 27 buildToolsVersion '27.0.3' defaultConfig { applicationId "com.finddreams.multilanguage" minSdkVersion 19 - targetSdkVersion 26 + targetSdkVersion 27 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" @@ -21,7 +21,7 @@ android { dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') - implementation 'com.android.support:appcompat-v7:26.1.0' + implementation 'com.android.support:appcompat-v7:27.1.1' implementation 'com.android.support.constraint:constraint-layout:1.0.2' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.1' diff --git a/app/src/main/java/com/finddreams/multilanguage/BaseActivity.java b/app/src/main/java/com/finddreams/multilanguage/BaseActivity.java index 1bc6154..255432e 100644 --- a/app/src/main/java/com/finddreams/multilanguage/BaseActivity.java +++ b/app/src/main/java/com/finddreams/multilanguage/BaseActivity.java @@ -1,12 +1,20 @@ package com.finddreams.multilanguage; +import android.content.Context; +import android.content.SharedPreferences; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; +import com.finddreams.languagelib.MultiLanguageUtil; + public class BaseActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(MultiLanguageUtil.attachBaseContext(newBase)); + } } diff --git a/app/src/main/java/com/finddreams/multilanguage/MainActivity.java b/app/src/main/java/com/finddreams/multilanguage/MainActivity.java index 9ef1e21..926a10a 100644 --- a/app/src/main/java/com/finddreams/multilanguage/MainActivity.java +++ b/app/src/main/java/com/finddreams/multilanguage/MainActivity.java @@ -6,6 +6,7 @@ import android.util.Log; import android.view.View; +import com.finddreams.languagelib.MultiLanguageUtil; import com.finddreams.languagelib.OnChangeLanguageEvent; import org.greenrobot.eventbus.EventBus; @@ -17,6 +18,7 @@ public class MainActivity extends BaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); +// MultiLanguageUtil.getInstance().setConfiguration(); setContentView(R.layout.activity_main); setTitle(R.string.app_name); EventBus.getDefault().register(this); diff --git a/app/src/main/java/com/finddreams/multilanguage/MyApplication.java b/app/src/main/java/com/finddreams/multilanguage/MyApplication.java index db7e7c3..6c0412f 100644 --- a/app/src/main/java/com/finddreams/multilanguage/MyApplication.java +++ b/app/src/main/java/com/finddreams/multilanguage/MyApplication.java @@ -1,8 +1,12 @@ package com.finddreams.multilanguage; +import android.annotation.TargetApi; import android.app.Activity; import android.app.Application; +import android.content.Context; +import android.content.SharedPreferences; import android.content.res.Configuration; +import android.os.Build; import android.os.Bundle; import com.finddreams.languagelib.MultiLanguageUtil; @@ -16,7 +20,7 @@ public class MyApplication extends Application{ public void onCreate() { super.onCreate(); MultiLanguageUtil.init(this); - MultiLanguageUtil.getInstance().setConfiguration(); +// MultiLanguageUtil.getInstance().setConfiguration(); registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() { @Override public void onActivityCreated(Activity activity, Bundle bundle) { @@ -30,7 +34,7 @@ public void onActivityStarted(Activity activity) { @Override public void onActivityResumed(Activity activity) { - MultiLanguageUtil.getInstance().setConfiguration(); +// MultiLanguageUtil.getInstance().setConfiguration(); } @Override @@ -58,6 +62,8 @@ public void onActivityDestroyed(Activity activity) { @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); - MultiLanguageUtil.getInstance().setConfiguration(); +// MultiLanguageUtil.getInstance().setConfiguration(); } + + } diff --git a/app/src/main/java/com/finddreams/multilanguage/SetLanguageActivity.java b/app/src/main/java/com/finddreams/multilanguage/SetLanguageActivity.java index d66a9b9..ad550d9 100644 --- a/app/src/main/java/com/finddreams/multilanguage/SetLanguageActivity.java +++ b/app/src/main/java/com/finddreams/multilanguage/SetLanguageActivity.java @@ -88,7 +88,7 @@ public void onClick(View view) { intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(intent); if (selectedLanguage == LanguageType.LANGUAGE_FOLLOW_SYSTEM) { - System.exit(0); +// System.exit(0); } } diff --git a/app/src/main/res/values-zh-rHK/strings.xml b/app/src/main/res/values-zh-rHK/strings.xml deleted file mode 100644 index d4da23f..0000000 --- a/app/src/main/res/values-zh-rHK/strings.xml +++ /dev/null @@ -1,12 +0,0 @@ - - Android多語言 - 你好,我是尋夢,我的博客地址是:http://blog.csdn.net/finddreams,希望大家多多關注! - 打開橫屏頁面 - 跟隨系統 - 打開webview頁面 - 多語言設定 - 多語言設定 - 打開二級頁面測試是否异常 - 這是橫屏頁面 - - diff --git a/build.gradle b/build.gradle index 020eae2..eccff49 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.0.0' + classpath 'com.android.tools.build:gradle:3.1.1' // NOTE: Do not place your application dependencies here; they belong diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 3b5e81d..6f1cd68 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip diff --git a/languagelib/build.gradle b/languagelib/build.gradle index e477807..e616cca 100644 --- a/languagelib/build.gradle +++ b/languagelib/build.gradle @@ -1,11 +1,11 @@ apply plugin: 'com.android.library' android { - compileSdkVersion 26 + compileSdkVersion 27 buildToolsVersion '27.0.3' defaultConfig { minSdkVersion 19 - targetSdkVersion 26 + targetSdkVersion 27 versionCode 1 versionName "1.0" @@ -22,7 +22,7 @@ android { dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') - implementation 'com.android.support:appcompat-v7:26.1.0' + implementation 'com.android.support:appcompat-v7:27.1.1' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.1' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' diff --git a/languagelib/src/main/java/com/finddreams/languagelib/MultiLanguageUtil.java b/languagelib/src/main/java/com/finddreams/languagelib/MultiLanguageUtil.java index 0af0c16..be46405 100644 --- a/languagelib/src/main/java/com/finddreams/languagelib/MultiLanguageUtil.java +++ b/languagelib/src/main/java/com/finddreams/languagelib/MultiLanguageUtil.java @@ -1,9 +1,11 @@ package com.finddreams.languagelib; +import android.annotation.TargetApi; import android.content.Context; import android.content.res.Configuration; import android.content.res.Resources; import android.os.Build; +import android.os.LocaleList; import android.text.TextUtils; import android.util.DisplayMetrics; import android.util.Log; @@ -56,9 +58,9 @@ public void setConfiguration() { } else { configuration.locale = targetLocale; } - Resources resources = mContext.getResources(); - DisplayMetrics dm = resources.getDisplayMetrics(); - resources.updateConfiguration(configuration, dm);//语言更换生效的代码! + Resources resources = mContext.getResources(); + DisplayMetrics dm = resources.getDisplayMetrics(); + resources.updateConfiguration(configuration, dm);//语言更换生效的代码! } /** @@ -69,18 +71,8 @@ public void setConfiguration() { private Locale getLanguageLocale() { int languageType = CommSharedUtil.getInstance(mContext).getInt(MultiLanguageUtil.SAVE_LANGUAGE, 0); if (languageType == LanguageType.LANGUAGE_FOLLOW_SYSTEM) { - Locale sysType = getSysLocale(); - if (sysType.equals(Locale.ENGLISH)) { - return Locale.ENGLISH; - } else if (sysType.equals(Locale.TRADITIONAL_CHINESE)) { - return Locale.TRADITIONAL_CHINESE; - } else if (TextUtils.equals(sysType.getLanguage(), Locale.CHINA.getLanguage())) { //zh - if (TextUtils.equals(sysType.getCountry(), Locale.CHINA.getCountry())) { //适配华为mate9 zh_CN_#Hans - return Locale.SIMPLIFIED_CHINESE; - } - } else { - return Locale.SIMPLIFIED_CHINESE; - } + Locale sysLocale= getSysLocale(); + return sysLocale; } else if (languageType == LanguageType.LANGUAGE_EN) { return Locale.ENGLISH; } else if (languageType == LanguageType.LANGUAGE_CHINESE_SIMPLIFIED) { @@ -88,8 +80,8 @@ private Locale getLanguageLocale() { } else if (languageType == LanguageType.LANGUAGE_CHINESE_TRADITIONAL) { return Locale.TRADITIONAL_CHINESE; } - Log.e(TAG, "getLanguageLocale" + languageType + languageType); getSystemLanguage(getSysLocale()); + Log.e(TAG, "getLanguageLocale" + languageType + languageType); return Locale.SIMPLIFIED_CHINESE; } @@ -98,13 +90,15 @@ private String getSystemLanguage(Locale locale) { } - //7.0以上获取方式需要特殊处理一下 + //以上获取方式需要特殊处理一下 public Locale getSysLocale() { - if (Build.VERSION.SDK_INT < 24) { - return mContext.getResources().getConfiguration().locale; + Locale locale; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + locale = LocaleList.getDefault().get(0); } else { - return mContext.getResources().getConfiguration().getLocales().get(0); + locale = Locale.getDefault(); } + return locale; } /** @@ -146,4 +140,22 @@ public int getLanguageType() { Log.e(TAG, "getLanguageType" + languageType); return languageType; } + + public static Context attachBaseContext(Context context) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + return createConfigurationResources(context); + } else { + MultiLanguageUtil.getInstance().setConfiguration(); + return context; + } + } + + @TargetApi(Build.VERSION_CODES.N) + private static Context createConfigurationResources(Context context) { + Resources resources = context.getResources(); + Configuration configuration = resources.getConfiguration(); + Locale locale=getInstance().getLanguageLocale(); + configuration.setLocale(locale); + return context.createConfigurationContext(configuration); + } }