【问题标题】:How to save the configuration of widgets如何保存小部件的配置
【发布时间】:2014-04-24 10:10:46
【问题描述】:

我正忙着关注this android tutorial on widgets

尤其是您设置ConfigurationActivty 的部分。

这是他们的步骤:

  1. 首先,从启动 Activity 的 Intent 中获取 App Widget ID
  2. 执行您的应用小部件配置。
  3. 配置完成后,通过调用AppWidgetManager.getInstance()获取AppWidgetManager的实例
  4. 通过调用updateAppWidget(int, RemoteViews) 使用RemoteViews 布局更新应用小部件
  5. 最后,创建返回Intent,用Activity结果设置它,完成Activity

我需要 2 方面的帮助:据我观察,人们正在使用 SharedPrefs,但是我如何实际访问提供有关小部件信息的 XML,例如更新频率:

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialLayout="@layout/widget_layout"
    android:minHeight="200dp"
    android:minWidth="144dp"
    android:widgetCategory="home_screen"
    android:configure="widget.AppWidgetConfigureActivity"
    android:updatePeriodMillis="1800000" >

</appwidget-provider>

{Edit:} 好的,到目前为止我已经实现了这个:

    private void SaveWidgetConfiguration() {

    int deviceTypeId = 0;
    int deviceId = 0;
    String hashedPasscode = "";
    int updateFreq = 30000;

    SharedPreferences prefs = AppWidgetConfigureActivity.this.getSharedPreferences("prefs", 0);
    SharedPreferences.Editor edit = prefs.edit();
    edit.putInt("Widget_DeviceTypeId:" + appWidgetId, deviceTypeId);
    edit.putLong("Widget_DeviceId:" + appWidgetId, deviceId);
    edit.putString("Widget_Passcode:" + appWidgetId, hashedPasscode);
    edit.putInt("Widget_UpdateFreq:" + appWidgetId, updateFreq);
    edit.commit();
}

但是现在我在哪里以及如何获得这些偏好?

我正在使用一项服务来更新我的小部件。我可以在 MyWidgetProvider 中获取它们吗?

我当前的 MyWidgetProvider:

public class MyWidgetProvider extends AppWidgetProvider {

private static final String LOG = "de.vogella.android.widget.example";

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

    Log.w(LOG, "onUpdate method called");
    // Get all ids
    ComponentName thisWidget = new ComponentName(context, MyWidgetProvider.class);
    int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

    // Get Preferences:

    // Build the intent to call the service
    Intent intent = new Intent(context.getApplicationContext(), UpdateWidgetService.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);

    // Update the widgets via the service
    context.startService(intent);
}

【问题讨论】:

  • 你试过我的解决方案了吗?

标签: android android-widget android-appwidget


【解决方案1】:

App Widget 配置 Activity 是一个可选的 Activity,它在用户添加您的 App Widget 时启动,并允许他或她在创建时修改 App Widget 设置。在配置Activity中你可以设置更新频率、TextView中的文本、widget的背景drawable等等……这意味着你在widget被创建和可见之前就对其进行了设置。您可以将所有设置保存在 SharedPreferences 中,如果您需要更新或重新创建小部件(在重新启动或更改配置后),您可以在 onUpdateonUpdate 方法或您的 SharedPreferences 中获取保存的设置@服务UpdateWidgetService.java。由于您使用UpdateWidgetService 更新您的小部件,因此您应该在此服务中使用SharedPreferences

这是一个例子:

Context context = getApplicationContext();

SharedPreferences prefs = context.getSharedPreferences("prefs", 0);
int deviceTypeId = prefs.getInt("Widget_DeviceTypeId:" + appWidgetId, defValue); // defValue is used if the preference doesn't exist
// get all other preferences/settings and use them to update the widget

如果您需要更多详细信息,请询问。

【讨论】:

  • 嘿,如果我将配置保存在共享首选项中,onUpdate 是加载它们并将它们设置回我的小部件的正确位置?
【解决方案2】:

您无需访问appwidget_info.xml,因为它由所有小部件共享,并且您已在Manifest 处声明它的用途。它将被WidgetProvider 自动提取。 Perform your App Widget configuration. stage 可用于从SharedPrefences 中提取一些设置,可用于稍后创建视图。

【讨论】:

  • 请看我的编辑。然后我是否在我的 WidgetProviderBoarcast 中查找首选项,然后根据参数生成适当的布局?
猜你喜欢
  • 1970-01-01
  • 2020-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-16
  • 1970-01-01
  • 2010-11-23
  • 1970-01-01
相关资源
最近更新 更多