【发布时间】:2011-08-04 21:24:46
【问题描述】:
当我得到偏好时有什么区别:
PreferenceManager.getDefaultSharedPreferences(getBaseContext());
和
getPreferences(Context.MODE_PRIVATE);
【问题讨论】:
标签: android android-preferences android-sharedpreferences
当我得到偏好时有什么区别:
PreferenceManager.getDefaultSharedPreferences(getBaseContext());
和
getPreferences(Context.MODE_PRIVATE);
【问题讨论】:
标签: android android-preferences android-sharedpreferences
PreferenceManager.getDefaultSharedPreferences(getBaseContext());
将提供对整个应用程序包的全局首选项文件的访问;任何活动都可以访问首选项(在内部,保存首选项的 xml 文件将命名为your.application.package_preferences.xml)。
contextInstance.getPreferences(Context.MODE_PRIVATE);
将只为 contextInstance 类提供首选项:只有上下文类的实例可以访问这些首选项(假设您的包仍然是 your.application.package 而您在 your.application.package.foo.MyActivity 中,内部首选项文件是 foo.MyActivity.xml)。
【讨论】:
.getDefaultSharedPreferences(getBaseContext()),您能否也澄清一下,其他应用程序是否能够编辑此值?
getDefaultSharedPreferences(Context context) 会在Context.MODE_PRIVATE 中为您提供偏好(根据android.git.kernel.org/?p=platform/frameworks/…)。也许您可以尝试类似该线程上一篇文章groups.google.com/group/android-developers/browse_thread/thread/… 中解释的内容。自己没试过,希望对你有帮助。