Skip to content

Commit

Permalink
修复使用夜间模式和屏幕旋转之后 Application 没有跟随变化的 Bug
Browse files Browse the repository at this point in the history
  • Loading branch information
getActivity committed Apr 20, 2021
1 parent 2b7e80f commit 615a890
Show file tree
Hide file tree
Showing 14 changed files with 118 additions and 70 deletions.
Binary file modified MultiLanguages.apk
Binary file not shown.
Binary file modified MultiLanguages.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,24 @@

#### 集成步骤

```groovy
buildscript {
......
}
allprojects {
repositories {
// JitPack 远程仓库:https://jitpack.io
maven { url 'https://jitpack.io' }
}
}
```

* 在项目 app 模块下的 `build.gradle` 文件中加入

```groovy
dependencies {
// 语种切换框架:https://github.com/getActivity/MultiLanguages
implementation 'com.hjq:language:6.5'
implementation 'com.github.getActivity:MultiLanguages:6.6'
}
```

Expand All @@ -25,7 +39,7 @@ public final class XxxApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// 初始化语种切换框架(自动适配第三方库中 Activity 语种)
// 初始化语种切换框架
MultiLanguages.init(this);
}
}
Expand Down
20 changes: 13 additions & 7 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@ android {
applicationId "com.hjq.language.demo"
minSdkVersion 16
targetSdkVersion 30
versionCode 65
versionName "6.5"
versionCode 66
versionName "6.6"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

// 支持 JDK 1.8
compileOptions {
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_1_8
}

buildTypes {
release {
minifyEnabled false
Expand All @@ -27,9 +34,8 @@ dependencies {
implementation 'com.google.android.material:material:1.2.1'

// 标题栏框架:https://github.com/getActivity/TitleBar
implementation 'com.hjq:titlebar:8.2'
// 吐司框架:https://github.com/getActivity/ToastUtils
implementation 'com.hjq:toast:8.8'
// 内存泄漏捕捉:https://github.com/square/leakcanary
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.5'
implementation 'com.github.getActivity:TitleBar:8.5'

// 内存泄漏检测:https://github.com/square/leakcanary
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.7'
}
4 changes: 1 addition & 3 deletions app/src/main/java/com/hjq/language/demo/AppApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import com.hjq.language.MultiLanguages;
import com.hjq.language.OnLanguageListener;
import com.hjq.toast.ToastUtils;

import java.util.Locale;

Expand All @@ -21,9 +20,8 @@ public final class AppApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
ToastUtils.init(this);

// 初始化多语种框架(自动适配第三方库中 Activity 语种)
// 初始化多语种框架
MultiLanguages.init(this);
// 设置语种变化监听器
MultiLanguages.setOnLanguageListener(new OnLanguageListener() {
Expand Down
41 changes: 27 additions & 14 deletions app/src/main/java/com/hjq/language/demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.hjq.language.MultiLanguages;
Expand All @@ -20,16 +19,19 @@
* desc : 多语种切换演示
*/
public final class MainActivity extends BaseActivity
implements View.OnClickListener {
implements RadioGroup.OnCheckedChangeListener {

private WebView mWebView;
private RadioGroup mRadioGroup;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_language);

mWebView = findViewById(R.id.wv_language_web);
mRadioGroup = findViewById(R.id.rg_language_languages);

mWebView.setWebViewClient(new WebViewClient());
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.loadUrl("https://developer.android.google.cn/index.html");
Expand All @@ -38,31 +40,42 @@ protected void onCreate(Bundle savedInstanceState) {
((TextView) findViewById(R.id.tv_language_application)).setText(getApplication().getResources().getString(R.string.current_language));
((TextView) findViewById(R.id.tv_language_system)).setText(MultiLanguages.getLanguageString(this, MultiLanguages.getSystemLanguage(), R.string.current_language));

findViewById(R.id.btn_language_auto).setOnClickListener(this);
findViewById(R.id.btn_language_cn).setOnClickListener(this);
findViewById(R.id.btn_language_tw).setOnClickListener(this);
findViewById(R.id.btn_language_en).setOnClickListener(this);
if (MultiLanguages.isSystemLanguage()) {
mRadioGroup.check(R.id.rb_language_auto);
} else {
Locale locale = MultiLanguages.getAppLanguage();
if (Locale.CHINA.equals(locale)) {
mRadioGroup.check(R.id.rb_language_cn);
} else if (Locale.TAIWAN.equals(locale)) {
mRadioGroup.check(R.id.rb_language_tw);
} else if (Locale.ENGLISH.equals(locale)) {
mRadioGroup.check(R.id.rb_language_en);
} else {
mRadioGroup.check(R.id.rb_language_auto);
}
}

mRadioGroup.setOnCheckedChangeListener(this);
}

/**
* {@link View.OnClickListener}
* {@link RadioGroup.OnCheckedChangeListener}
*/
@Override
public void onClick(View v) {
public void onCheckedChanged(RadioGroup group, int checkedId) {
// 是否需要重启
boolean restart = false;

int viewId = v.getId();
if (viewId == R.id.btn_language_auto) {
if (checkedId == R.id.rb_language_auto) {
// 跟随系统
restart = MultiLanguages.setSystemLanguage(this);
} else if (viewId == R.id.btn_language_cn) {
} else if (checkedId == R.id.rb_language_cn) {
// 简体中文
restart = MultiLanguages.setAppLanguage(this, Locale.CHINA);
} else if (viewId == R.id.btn_language_tw) {
} else if (checkedId == R.id.rb_language_tw) {
// 繁体中文
restart = MultiLanguages.setAppLanguage(this, Locale.TAIWAN);
} else if (viewId == R.id.btn_language_en) {
} else if (checkedId == R.id.rb_language_en) {
// 英语
restart = MultiLanguages.setAppLanguage(this, Locale.ENGLISH);
}
Expand Down
37 changes: 19 additions & 18 deletions app/src/main/res/layout/activity_language.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,56 +13,57 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:backButton="false"
app:title="https://github.com/getActivity/MultiLanguages"
app:titleSize="13sp" />
app:title="@string/app_name" />

<com.hjq.language.demo.LanguagesWebView
android:id="@+id/wv_language_web"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_marginTop="20dp"
android:layout_marginTop="10dp"
android:layout_weight="1" />

<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#dcdcdc" />

<LinearLayout
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
<RadioGroup
android:id="@+id/rg_language_languages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:orientation="horizontal">

<Button
android:id="@+id/btn_language_auto"
<RadioButton
android:id="@+id/rb_language_auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="自动" />

<Button
android:id="@+id/btn_language_cn"
<RadioButton
android:id="@+id/rb_language_cn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="简体" />

<Button
android:id="@+id/btn_language_tw"
<RadioButton
android:id="@+id/rb_language_tw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="繁体" />

<Button
android:id="@+id/btn_language_en"
<RadioButton
android:id="@+id/rb_language_en"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="英语" />

</LinearLayout>
</RadioGroup>

<LinearLayout
android:layout_width="wrap_content"
Expand All @@ -86,7 +87,7 @@
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_marginTop="20dp"
android:orientation="horizontal">

<TextView
Expand All @@ -104,8 +105,8 @@
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_marginBottom="40dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:orientation="horizontal">

<TextView
Expand Down
4 changes: 1 addition & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.0'
classpath 'com.novoda:bintray-release:0.9.2'
}
}

allprojects {
repositories {
maven {url "https://jitpack.io"}
jcenter()
google()
maven {url "https://jitpack.io"}
maven {url 'https://maven.google.com'}
}
}

Expand Down
14 changes: 2 additions & 12 deletions library/build.gradle
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
apply plugin: 'com.android.library'
apply plugin: 'com.novoda.bintray-release'

android {
compileSdkVersion 26

defaultConfig {
minSdkVersion 14
versionCode 65
versionName "6.5"
versionCode 66
versionName "6.6"
}
}

publish {
userOrg = 'getactivity'
groupId = 'com.hjq'
artifactId = 'language'
publishVersion = '6.5'
desc = 'Android Multi Languages Adaptation Framework'
website = "https://github.com/getActivity/MultiLanguages"
}

tasks.withType(Javadoc) {
options.addStringOption('Xdoclint:none', '-quiet')
options.addStringOption('encoding', 'UTF-8')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* author : Android 轮子哥
* github : https://github.com/getActivity/MultiLanguages
* time : 2021/01/21
* desc : Activity 语种绑定
* desc : Activity 语种注入
*/
final class ActivityLanguages implements Application.ActivityLifecycleCallbacks {

Expand Down
6 changes: 4 additions & 2 deletions library/src/main/java/com/hjq/language/LanguagesConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import java.util.Locale;

Expand All @@ -17,7 +18,8 @@ final class LanguagesConfig {
private static final String KEY_COUNTRY = "key_country";

private static String sSharedPreferencesName = "language_setting";
private static Locale sCurrentLanguage;

private static volatile Locale sCurrentLanguage;

static void setSharedPreferencesName(String name) {
sSharedPreferencesName = name;
Expand All @@ -39,7 +41,7 @@ static Locale getAppLanguage(Context context) {
if (sCurrentLanguage == null) {
String language = getSharedPreferences(context).getString(KEY_LANGUAGE, null);
String country = getSharedPreferences(context).getString(KEY_COUNTRY, null);
if (language != null && !"".equals(language)) {
if (!TextUtils.isEmpty(language)) {
sCurrentLanguage = new Locale(language, country);
} else {
sCurrentLanguage = LanguagesUtils.getLocale(context);
Expand Down
12 changes: 8 additions & 4 deletions library/src/main/java/com/hjq/language/LanguagesObserver.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.hjq.language;

import android.app.Application;
import android.content.ComponentCallbacks;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;

Expand Down Expand Up @@ -33,18 +33,22 @@ static Locale getSystemLanguage() {
/**
* 注册系统语种变化监听
*/
static void register(Context context) {
context.registerComponentCallbacks(new LanguagesObserver());
static void register(Application application) {
application.registerComponentCallbacks(new LanguagesObserver());
}

/**
* 手机的语种发生了变化
* 手机的配置发生了变化
*/
@Override
public void onConfigurationChanged(Configuration newConfig) {
Locale newLocale = LanguagesUtils.getLocale(newConfig);

Locale oldLocale = sSystemLanguage;

// 更新 Application 的配置,否则会出现横竖屏切换之后 Application 的 orientation 没有随之变化的问题
LanguagesUtils.updateConfigurationChanged(MultiLanguages.getApplication(), newConfig);

if (newLocale.equals(oldLocale)) {
return;
}
Expand Down
Loading

0 comments on commit 615a890

Please sign in to comment.