问题描述
我对context.MODE_PRIVATE
或MODE_READABLE, WRITABLE
的理解是,这些函数为共享首选项创建文件.
What I understood about context.MODE_PRIVATE
or MODE_READABLE, WRITABLE
is that those functions make files for sharedprefrences.
我想知道context.getSharedPreferences(KEY, Context.MODE_PRIVATE)
和getSharedPreferences(KEY, 0);
之间有什么区别.
I am wondering what the difference is between context.getSharedPreferences(KEY, Context.MODE_PRIVATE)
and getSharedPreferences(KEY, 0);
.
getSharedPreferences
从xml文件夹中检索其首选项.并且Context.MODE_PRIVATE
存储其文件.以及为什么同时使用context.getSharedPreferences(KEY, Context.MODE_PRIVATE)
和context.getSharedPreferences(KEY, Context.MODE_PRIVATE)
创建文件的原因,使用context.getSharedPreferences(KEY, Context.MODE_PRIVATE)
.
getSharedPreferences
retrieves its preferences from a xml folder as far as I know. And Context.MODE_PRIVATE
stores its files. And why use context.getSharedPreferences(KEY, Context.MODE_PRIVATE)
if both getSharedPreferences(KEY, 0)
and context.getSharedPreferences(KEY, Context.MODE_PRIVATE)
makes files.
下面是我注意到Context.MODE_PRIVATE
的Facebook API的一部分.
Below is part of the Facebook API where I noticed Context.MODE_PRIVATE
.
public static boolean save(Facebook session, Context context) {
Editor editor =
context.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
editor.putString(TOKEN, session.getAccessToken());
editor.putLong(EXPIRES, session.getAccessExpires());
return editor.commit();
}
public static boolean restore(Facebook session, Context context) {
SharedPreferences savedSession =
context.getSharedPreferences(KEY, Context.MODE_PRIVATE);
session.setAccessToken(savedSession.getString(TOKEN, null));
session.setAccessExpires(savedSession.getLong(EXPIRES, 0));
return session.isSessionValid();
}
推荐答案
根据 javadoc .因此,我假设您正在谈论Context.MODE_WORLD_WRITABLE
或Context.MODE_WORLD_READABLE
. (并不是说这实际上与您的问题有关……)
There is no Context.MODE_WRITABLE
or Context.MODE_READABLE
according to the javadoc. So I assume that you are talking about Context.MODE_WORLD_WRITABLE
or Context.MODE_WORLD_READABLE
. (Not that this is actually relevant to your question ...)
context.getSharedPreferences(KEY, Context.MODE_PRIVATE)
和
context.getSharedPreferences(KEY, 0);
没有功能上的区别. Context.MODE_PRIVATE
是值为零的int
常量;有关详细信息,请参考上面链接的javadoc.尽管前者更具可读性,但从代码风格的角度来看,它更可取.
There is no functional difference. Context.MODE_PRIVATE
is an int
constant with value zero; refer to the javadoc linked above for the details. The former is more readable though, and that makes it preferable from a code style perspective.
这篇关于为什么要使用Context.MODE_PRIVATE或Context.MODE_WRITABLE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!