本文介绍了获得preferences在AppWidget提供商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎遇到了麻烦阅读$ P $从我AppWidgetProvider类pferences。我的code工作在一个活动,但它不会在AppWidgetProvider。这里是code我使用读回一个布尔值:

I seem to be having trouble reading preferences from my AppWidgetProvider class. My code works in an Activity, but it does not in an AppWidgetProvider. Here is the code I am using to read back a boolean:

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean autoreplyon = settings.getBoolean("autoreplyon", false);

不过,我得到了的方法getShared preferences(字符串,INT)是未定义的类型的widget的错误(widget是我AppWidgetProvider类的名称)。

However, I get the "The method getSharedPreferences(String, int) is undefined for the type widget" error (widget is the name of my AppWidgetProvider class).

在此先感谢您的任何建议!

Thanks in advance for any suggestions!

推荐答案

getShared preferences(),如果您选择使用它,仅适用于子上下文,像活动的服务 AppWidgetProvider 的BroadcastReceiver 的子类,这是不是一个上下文

getSharedPreferences(), should you choose to use it, is only available on subclasses of Context, like Activity or Service. AppWidgetProvider is a subclass of BroadcastReceiver, which is not a Context.

话虽这么说,如果你要使用 preferenceScreen 系统,或者如果您不能确定它的得得得是 getShared preferences(),我会用 preferenceManager.getDefaultShared preferences()代替。这些都是共享preferences preferenceScreen / preferenceActivity 将使用。

That being said, if you are going to use the PreferenceScreen system, or if you are not certain that it's gotta gotta gotta be getSharedPreferences(), I would use PreferenceManager.getDefaultSharedPreferences() instead. Those are the SharedPreferences that PreferenceScreen/PreferenceActivity will use.

这篇关于获得preferences在AppWidget提供商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 05:47