【问题标题】:get SharedPreferences value in a non activity file in android?在 android 的非活动文件中获取 SharedPreferences 值?
【发布时间】: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类中获取价值?

【问题讨论】:

标签: android android-sharedpreferences


【解决方案1】:

使用下面的代码;

 sharedPreferences = context.getSharedPreference("MyPREFERENCES", MODE_PRIVATE);

【讨论】:

  • 这里 MODE_PRIVATE is not resolved 正在生成错误。
  • 写这个 Context.MODE_PRIVATE
  • 如果这对您有帮助,请勾选我的答案
猜你喜欢
  • 1970-01-01
  • 2021-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多