myLocale = new Locale(lang);Resources res = getResources();displayMetrics dm = res.getdisplayMetrics();Configuration conf = res.getConfiguration();conf.locale = myLocale;res.updateConfiguration(conf,dm);/* * Intent refresh = new Intent(this,AndroIDLocalizeActivity.class); * startActivity(refresh); finish(); */PrefManager _p = new PrefManager(getApplicationContext(),PrefConfignames.LANGUAGE_APP_CONfig);_p.setStringValue(GeneralPrefKeynames.LANGUAGE,lang);Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackagename());i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_top);startActivity(i);setResult(RESulT_OK,null);finish();
我无法得到共享引用会发生什么(我在这个应用程序中使用了开放源代码安全指引(CodeProject article))
这里!!!
提前致谢
编辑#1
PrefManager.java
public class PrefManager { private SecurePreferences _securePreferences = null; public PrefManager(Context context,String Prefname) { _securePreferences = new SecurePreferences(context,Prefname); } public voID setStringValue(String key,String value) { _securePreferences.edit().putString(key,value).commit(); }}
编辑#2
令人奇怪的是,当我从eclipse调试或运行应用程序(调试或作为AndroID应用程序运行)时,存储在sharedpref中的应用程序的状态具有正确的值!!!但是当我在手机上重新运行它时有默认值!!!请帮我解决这个问题!
编辑#3
我发现使用以下行重新启动应用程序存在问题,是否有另一种重新启动应用程序的方法没有任何问题?甚至更好的方式(更新语言,而不重新启动应用程序)?
Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackagename());i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_top);startActivity(i);
我尝试了以下链接来实现第二个提到的方法
Changing locale: Force activity to reload resources?
,但不是在以下行之后提高
res.updateConfiguration(conf,dm);
提前致谢
解决方法 我终于得到了解决方案,在onPause方法中,我将当前保存的语言保存在sharedprefs中,并在更改设置活动中的语言环境并返回到onResume方法中的上一个活动后,将新保存的lang与保存在onPause,如果这两个不相等,我将重新创建方法.@OverrIDeprotected voID onResume(){ super.onResume(); SharedPreferences _sPreferences = getSharedPreferences(PrefConfignames.LANGUAGE_APP_CONfig,Context.MODE_PRIVATE); String resumeLang = _sPreferences.getString(GeneralPrefKeynames.LANGUAGE,"En"); if (!currentLang.equals(resumeLang)) recreate();};@OverrIDeprotected voID onPause(){ SharedPreferences _sPreferences = getSharedPreferences(PrefConfignames.LANGUAGE_APP_CONfig,Context.MODE_PRIVATE); currentLang = _sPreferences.getString(GeneralPrefKeynames.LANGUAGE,"En"); super.onPause();};总结
以上是内存溢出为你收集整理的在设置Android应用程序的区域设置后,SharedPrefs将重置全部内容,希望文章能够帮你解决在设置Android应用程序的区域设置后,SharedPrefs将重置所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)