Android的getDefaultShared preferences [英] Android getDefaultSharedPreferences

查看:287
本文介绍了Android的getDefaultShared preferences的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的code是:

final String eulaKey = "mykey";
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean hasBeenShown = prefs.getBoolean(eulaKey, false);

始终返回根据操作系统版本不同的值。经过测试,在2.2,2.3.4,3.2,4.0.3 - 返回正确的值。但对于设备中兴刀片2.3.7与CianogenMod 7.1 - 结果始终是假的。我想:对于getBoolean默认值。

Always returns different values depending on os version. Tested in 2.2, 2.3.4, 3.2, 4.0.3 - returns correct value. But for device Zte blade with 2.3.7 with CianogenMod 7.1 - result is always false. I suppose default value for getBoolean.

下面是code写布尔:

Here is code writing boolean:

final String eulaKey = "mykey";
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(eulaKey, true);
editor.commit();

没有任何人有任何想法?

Does anybody have any idea?

更新: 比较我目前的code与我的previous的code版本 - 没有在code没有什么区别。 唯一的区别是明显的:code工程确定与minVersion = 8,targetVersion = 8 现在,我与编译minversion = 13 /因为Admob的/的= 8和目标。 也许一些API变化,但我什么也没发现这一点。

Update: Comparing my current code with my previous version of code - there is no difference in code. Only difference is in manifest: code works Ok with minVersion=8 and targetVersion=8 Now I'm compiling with minversion=8 and target=13 /because of Admob/. Maybe some APIs changed, but I found nothing on this.

解决方案: 从快捷方式,并从菜单中-Starting应用程序给了我不同的DefaultShared preferences。从我的code删除DefaultShared preferences后 - 它的作品完美。我不能随便说:人不犯快捷键,所以我不得不改变code

SOLUTION: -Starting app from shortcut and from menu gives me different DefaultSharedPreferences. After removing DefaultSharedPreferences from my code - it works perfect. I can't just say: people don't make shortcuts, so I had to change code.

推荐答案

试试这样说:

    final String eulaKey = "mykey";
    Context mContext = getApplicationContext();
    mPrefs = mContext.getSharedPreferences("myAppPrefs", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = mPrefs.edit();
    editor.putBoolean(eulaKey, true);
    editor.commit();

在这种情况下,您可以指定自己的preferences文件名(对myApp preFS),并可以控制访问persmission它。其它操作模式包括:MODE_WORLD_READABLE,MODE_WORLD_WRITEABLE,MODE_MULTI_PROCESS

in which case you can specify your own preferences file name (myAppPrefs) and can control access persmission to it. Other operating modes include: MODE_WORLD_READABLE, MODE_WORLD_WRITEABLE, MODE_MULTI_PROCESS

这篇关于Android的getDefaultShared preferences的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆