Android的 - 如何使用共享preferences非活动课? [英] Android - How to use SharedPreferences in non-Activity class?

查看:167
本文介绍了Android的 - 如何使用共享preferences非活动课?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用共享preferences在非活动课?我试图使一个通用的preferences工具类和进口 android.content.Context 但是Eclipse还是不肯让我用 getShared preferences()

How do you use SharedPreferences in a non-Activity class? I tried making a generic Preferences utility class and importing android.content.Context but Eclipse still wouldn't let me use getSharedPreferences().

推荐答案

共享preferenced都涉及到环境。 您只能通过上下文引用它。

SharedPreferenced are related to context. You can only reference it through a context.

您可以简单地通过上下文作为参数传递给你的班级。 例如,在构造函数中。

You can simply pass context as a parameter to your class. For example in the constructor.

在你的活动做的:

MyClass myClass = new MyClass(this);

这篇关于Android的 - 如何使用共享preferences非活动课?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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