小部件:存储用户偏好
我正在尝试制作一个小部件,用户必须为其提供名称。根据该名称,收集并显示数据。小部件中有一个刷新按钮,用于刷新此数据。
问题在于配置类和 AppWidgetProvider 类之间共享此名称。我尝试过:
- 只需在小部件中使用 EditText 即可。在 config 类中,在 EditText 中设置名称,然后在 AppWidgetProvider 类中检索它。 这不起作用,因为我找不到在 AWP 中检索文本的方法。
- 使用 SharedPreferences:
在配置类中:
c = SelectWidgetStationActivity.this;
// Getting info about the widget that launched this Activity.
Intent i = getIntent();
Bundle extras = i.getExtras();
if (extras != null)
awID = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);
prefs.edit().putString("widgetname" + awID, name);
prefs.edit().commit();
在 AWP 类中:
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) { //ACTION_WIDGET_RECEIVER is the action fired by the refresh button
this.onUpdate(context, AppWidgetManager.getInstance(context), AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName("com.app.myapp", "com.app.myapp.MyWidgetProvider")));
}
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
for (int widgetId : appWidgetIds) {
name = prefs.getString("widgetname" + widgetId, "N/A"));
//more code
< code>name 一直给我“N/A”。我已检查 awID
和 widgetId
是否相等。 这可能是因为我使用了不同的上下文? (这里只是猜测)
那么有什么方法可以解决这个问题呢?
编辑 当我在屏幕上打印上下文时,我得到以下内容:
config class:
com.app.myapp.WidgetConfigActivity@40774d18
AWP 类:
android.app.ReceiverRestrictedContext@406ad290
I'm trying to make a widget, for which the user has to provide a name. Based on that name, data is collected and shown. In the widget is a refresh button to refresh this data.
The problem is sharing this name between the configuration class and the AppWidgetProvider class. What I have tried:
- Just use an EditText in the widget. In the config class, set the name in the EditText, and retrieve it in the AppWidgetProvider class. This doesn't work, because I can't find a way to retrieve the text in the AWP.
- Use SharedPreferences:
In the config class:
c = SelectWidgetStationActivity.this;
// Getting info about the widget that launched this Activity.
Intent i = getIntent();
Bundle extras = i.getExtras();
if (extras != null)
awID = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);
prefs.edit().putString("widgetname" + awID, name);
prefs.edit().commit();
In the AWP class:
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) { //ACTION_WIDGET_RECEIVER is the action fired by the refresh button
this.onUpdate(context, AppWidgetManager.getInstance(context), AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName("com.app.myapp", "com.app.myapp.MyWidgetProvider")));
}
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
for (int widgetId : appWidgetIds) {
name = prefs.getString("widgetname" + widgetId, "N/A"));
//more code
name
keeps giving me "N/A". I've checked that awID
and widgetId
are equal.
This is probably due to the fact that I'm using different contexts? (Just guessing here)
So what is a way to solve this problem?
Edit
When I'm printing the contexts on screen I get the following:
config class:
com.app.myapp.WidgetConfigActivity@40774d18
AWP class:
android.app.ReceiverRestrictedContext@406ad290
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
出于好奇,我再次浏览了一遍,并注意到了这一点:
这为您提供了 2 个不同的
Editor
实例。您在这里要做的就是找一位编辑器,输入首选项并保留它。然后你会得到一个新的(未更改的)编辑器并提交它(=没有写入任何更改)。刚刚在一个小项目上进行了测试,未按预期正确提交。
因此,尝试用这样的代码替换代码:
或将提交链接到
Just went through this again out of curiousity and noticed this:
This gives you 2 different
Editor
instances.What you do here is get one editor, put in the preference and leave it alone. Then you get a new (unchanged) editor and just commit it (= no changes are written). Just tested that on a small project, doesn't commit correctly as intended.
So try to substitute the code with something like this:
or chain the commit in
你可以尝试
You might try
编写首选项时,请确保
name
不为null
。否则,当再次读取它时,您将得到null
,这将返回默认值“N/A”(就像找不到密钥一样)。Make sure that
name
is notnull
when writing the preference. Otherwise you'll getnull
when reading it again which will return the default value "N/A" (like the key was not found).