Skip to content

Commit

Permalink
兼容Android8.0系统
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Apr 13, 2018
1 parent d1c0e4e commit 315e402
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 44 deletions.
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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'
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand All @@ -30,7 +34,7 @@ public void onActivityStarted(Activity activity) {

@Override
public void onActivityResumed(Activity activity) {
MultiLanguageUtil.getInstance().setConfiguration();
// MultiLanguageUtil.getInstance().setConfiguration();
}

@Override
Expand Down Expand Up @@ -58,6 +62,8 @@ public void onActivityDestroyed(Activity activity) {
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
MultiLanguageUtil.getInstance().setConfiguration();
// MultiLanguageUtil.getInstance().setConfiguration();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
12 changes: 0 additions & 12 deletions app/src/main/res/values-zh-rHK/strings.xml

This file was deleted.

2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions languagelib/build.gradle
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -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'
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);//语言更换生效的代码!
}

/**
Expand All @@ -69,27 +71,17 @@ 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) {
return Locale.SIMPLIFIED_CHINESE;
} 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;
}

Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 315e402

Please sign in to comment.