小部件:存储用户偏好

发布于 2024-12-14 04:42:01 字数 1900 浏览 3 评论 0原文

我正在尝试制作一个小部件,用户必须为其提供名称。根据该名称,收集并显示数据。小部件中有一个刷新按钮,用于刷新此数据。

问题在于配置类和 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”。我已检查 awIDwidgetId 是否相等。 这可能是因为我使用了不同的上下文? (这里只是猜测)

那么有什么方法可以解决这个问题呢?

编辑 当我在屏幕上打印上下文时,我得到以下内容:

  • 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼眸里的那抹悲凉 2024-12-21 04:42:01

出于好奇,我再次浏览了一遍,并注意到了这一点:

prefs.edit().putString("widgetname" + awID, name);
prefs.edit().commit();

这为您提供了 2 个不同的 Editor 实例。
您在这里要做的就是找一位编辑器,输入首选项并保留它。然后你会得到一个新的(未更改的)编辑器并提交它(=没有写入任何更改)。刚刚在一个小项目上进行了测试,未按预期正确提交。

因此,尝试用这样的代码替换代码:

Editor e = prefs.edit();
e.putString("widgetname" + awID, name);
e.commit();

或将提交链接到

prefs.edit().putString("widgetname" + awID, name).commit();

Just went through this again out of curiousity and noticed this:

prefs.edit().putString("widgetname" + awID, name);
prefs.edit().commit();

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:

Editor e = prefs.edit();
e.putString("widgetname" + awID, name);
e.commit();

or chain the commit in

prefs.edit().putString("widgetname" + awID, name).commit();
秋风の叶未落 2024-12-21 04:42:01

你可以尝试

SharedPreferences prefs = context.getSharedPreferences("someName", Context.MODE_PRIVATE);

You might try

SharedPreferences prefs = context.getSharedPreferences("someName", Context.MODE_PRIVATE);
扶醉桌前 2024-12-21 04:42:01
prefs.edit().putString("widgetname" + awID, name);

编写首选项时,请确保 name 不为 null。否则,当再次读取它时,您将得到 null ,这将返回默认值“N/A”(就像找不到密钥一样)。

prefs.edit().putString("widgetname" + awID, name);

Make sure that name is not null when writing the preference. Otherwise you'll get null when reading it again which will return the default value "N/A" (like the key was not found).

最近可好 2024-12-21 04:42:01
public static final String PREFS_NAME = "MyPrefsFile";
private static final String PREF_USERNAME = "username";


SharedPreferences pref = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
 //to access data from preference 

uid.setText(pref.getString(PREF_USERNAME, null));

//to set value in preference
   getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit()
                    .putString(PREF_USERNAME, uid.getText().toString()).commit();
public static final String PREFS_NAME = "MyPrefsFile";
private static final String PREF_USERNAME = "username";


SharedPreferences pref = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
 //to access data from preference 

uid.setText(pref.getString(PREF_USERNAME, null));

//to set value in preference
   getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit()
                    .putString(PREF_USERNAME, uid.getText().toString()).commit();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文