已签名的 apk 中的区域设置未更改
Locale not changing in signed apk
我有一个 android 应用程序,其中包含 4 种语言,即英语、印地语、古吉拉特语和马拉地语。
我的 res 文件夹中有 4 个字符串文件,当我 运行 将调试模式下的代码直接发送到我的设备时,效果很好。但是当我在 Playstore 上签署 apk 和 post 并尝试下载它时,它不起作用。
这是我设置语言环境的代码:
public class BaseActivity extends AppCompatActivity {
public SharedPreferences sharedpreferences;
public static final String MyPREFERENCES = "MyPrefs";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
String lang = sharedpreferences.getString("lang","en");
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! : "+lang);
Locale locale = new Locale(lang);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
}
}
我将此 BaseActivity 扩展到我使用的所有活动。
所以我无法弄清楚为什么它在调试中工作正常而不是在签名的 apk 中工作。
使用
getApplicationContext().getResources().updateConfiguration(config,
getApplicationContext().getResources().getDisplayMetrics());
而不是
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
因为
getApplicationContext() - Returns the context for all activities
running in application.
和
getBaseContext() - If you want to access Context from another context
within application you can access.
我有一个 android 应用程序,其中包含 4 种语言,即英语、印地语、古吉拉特语和马拉地语。 我的 res 文件夹中有 4 个字符串文件,当我 运行 将调试模式下的代码直接发送到我的设备时,效果很好。但是当我在 Playstore 上签署 apk 和 post 并尝试下载它时,它不起作用。
这是我设置语言环境的代码:
public class BaseActivity extends AppCompatActivity {
public SharedPreferences sharedpreferences;
public static final String MyPREFERENCES = "MyPrefs";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
String lang = sharedpreferences.getString("lang","en");
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! : "+lang);
Locale locale = new Locale(lang);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
}
}
我将此 BaseActivity 扩展到我使用的所有活动。 所以我无法弄清楚为什么它在调试中工作正常而不是在签名的 apk 中工作。
使用
getApplicationContext().getResources().updateConfiguration(config,
getApplicationContext().getResources().getDisplayMetrics());
而不是
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
因为
getApplicationContext() - Returns the context for all activities running in application.
和
getBaseContext() - If you want to access Context from another context within application you can access.