android 切换语言重启应用程序,Android 切换系统语言后,重启App

package com.barry.common.util;

import android.app.Activity;

import android.content.Intent;

import android.content.SharedPreferences;

import android.preference.PreferenceManager;

import android.text.TextUtils;

import java.util.Locale;

/**

* 切换语言的工具类

*

* @author : barry.huang

* @time : 16/9/21

**/

public class LanguageUtil {

private static final String LAST_LANGUAGE = “lastLanguage”;

/**

* 当改变系统语言时,重启App

*

* @param activity

* @param homeActivityCls 主activity

* @return

*/

public static boolean isLanguageChanged(Activity activity, Class> homeActivityCls) {

Locale locale = Locale.getDefault();

if (locale == null) {

return false;

}

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(activity);

String localeStr = sp.getString(LAST_LANGUAGE, “”);

String curLocaleStr = getLocaleString(locale);

if (TextUtils.isEmpty(localeStr)) {

sp.edit().putString(LAST_LANGUAGE, curLocaleStr).commit();

return false;

} else {

if (localeStr.equals(curLocaleStr)) {

return false;

} else {

sp.edit().putString(LAST_LANGUAGE, curLocaleStr).commit();

restartApp(activity, homeActivityCls);

return true;

}

}

}

private static String getLocaleString(Locale locale) {

if (locale == null) {

return “”;

} else {

return locale.getCountry() + locale.getLanguage();

}

}

public static void restartApp(Activity activity, Class> homeClass) {

Intent intent = new Intent(activity, homeClass);

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

activity.startActivity(intent);

// 杀掉进程

android.os.Process.killProcess(android.os.Process.myPid());

System.exit(0);

}

}在基类BaseActivity的onCreate()方法里调用LanguageUtil.isLanguageChanged(activity, homeActivityCls).添加权限over.

    原文作者:三水蜀黍
    原文地址: https://blog.csdn.net/weixin_30947859/article/details/117553850
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞