在非活动类中获取 SharedPreferences [英] Getting SharedPreferences in Non-Activity class

查看:80
本文介绍了在非活动类中获取 SharedPreferences的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下课程:

public class UserPref {
    Context context;

    SharedPreferences sharedPref;

    public SessionUtils(Context context) {
        this.context = context;
        sharedPref = context.getPreferences(Context.MODE_PRIVATE);
    }
}

但是,我在 getPreferences() 上收到以下错误:

However, I get the following error on getPreferences():

无法解析方法getPreferences(int)"

Cannot resolve method 'getPreferences(int)'

为什么?我该如何解决这个问题?

Why? How do I fix this?

推荐答案

就像

SharedPreferences preferences = context.getSharedPreferences("AppPreferences", Activity.MODE_PRIVATE);

更多相关信息官方文档

这篇关于在非活动类中获取 SharedPreferences的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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