【发布时间】:2017-08-04 06:13:18
【问题描述】:
我有一个包含所有常用功能的类,我想在那里获得一些 sharedpreferences 值。下面的代码 getSharedPreference() 给出错误。
这是我的代码:
public class CommonFunctions {
Context mContext;
SharedPreferences sharedPreferences;
public CommonFunctions(Context context) {
this.mContext = context;
sharedPreferences = getSharedPreference("MyPREFERENCES", MODE_PRIVATE);
}
}
有没有办法在java类中获取价值?
【问题讨论】:
-
你必须使用 context.getSharedPreference("MyPREFERENCES", context.MODE_PRIVATE);
-
不能在activity之外直接使用上下文相关的函数
标签: android android-sharedpreferences