【问题标题】:onGetViewFactory only called once for multiple widgetsonGetViewFactory 只为多个小部件调用一次
【发布时间】:2012-07-05 18:24:37
【问题描述】:

我有一个使用ListView 的应用程序小部件。 我创建了一个扩展 RemoteViewsService 的类:

public class AppWidgetService extends RemoteViewsService {
    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        return (new AppWidgetListFactory(this.getApplicationContext(), intent));
    }
}

在我的AppWidgetProvider 中,我为小部件的每个实例(对于每个appWidgetId)调用以下方法:

private static void fillInList(RemoteViews widget, Context context, int appWidgetId) {
    Intent intent = new Intent(context, AppWidgetService.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    widget.setRemoteAdapter(R.id.appwidget_listview, intent);
}

AppWidgetListFactory的构造函数

public AppWidgetListFactory(Context context, Intent intent) {
    this.context = context;
    appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);

    System.out.println("New TVAPWLF for: " + appWidgetId);
    trains = WidgetData.getInstance().widgetMap.get(appWidgetId);
}

现在的问题是,fillInList 方法的调用频率与小部件实例的调用频率一样,但 onGetViewFactory 方法只调用一次。这会导致所有小部件显示相同的数据。

我该如何解决这个问题?

【问题讨论】:

  • 您能再添加一些代码吗?如果您将fillInList 方法设为非静态方法,它会起作用吗?
  • 就我而言,intent.putExtra 确实有效。每个实例我都会获得正确的必要小部件 ID。

标签: android android-appwidget


【解决方案1】:

我自己没有对此进行测试,但是查看RemoteViewsService.onBind() 的源代码,我相信您不能仅仅改变 Intent 中的附加内容,以便它检测到对您的 onGetViewFactory() 方法的新调用需要,因为它使用Intent.filterEquals() 方法进行比较:

为了解决意图(过滤),确定两个意图是否相同。也就是说,如果它们的操作、数据、类型、类和类别相同。这不会比较意图中包含的任何额外数据。

我建议通过 Intent 的数据传递您需要的信息(小部件 ID?),可能类似于:

private static void fillInList(RemoteViews widget, Context context, int appWidgetId) {
    Intent intent = new Intent(context, AppWidgetService.class);
    intent.setData(Uri.fromParts("content", String.valueOf(appWidgetId), null));
    widget.setRemoteAdapter(R.id.appwidget_listview, intent);
}

分别在接收方:

public TreinVerkeerAppWidgetListFactory(Context context, Intent intent) {
    this.context = context;
    appWidgetId = Integer.valueOf(intent.getData().getSchemeSpecificPart());

    System.out.println("New TVAPWLF for: " + appWidgetId);
    trains = WidgetData.getInstance().widgetMap.get(appWidgetId);
}

【讨论】:

  • @Joe 能否请您检查一下如何在我的代码中执行此操作,实际上我按照您所说的进行了更改,但现在它崩溃了。这是我的代码pastie.org/7063960 这是logcat pastie.org/7063968
  • @Anshuman 我在你的第一个馅饼的第 36 行看到一个问题:String.valueOf(appWidgetIds)。这里appWidgetIds 是一个数组而不是单个整数,它会在意图中产生一个无效的URI(类似于“[I@424b7d58”而不是“2”)。你的意思是写String.valueOf(appWidgetIds[0])吗?
  • @Joe Yep 现在可以工作了。我还有一个问题。是否可以在代码中为应用小部件listView设置背景图片,而不是在xml文件中设置。
  • @Anshuman 最好发布一个新问题,因为这似乎是一个不相关的问题。确保您进行搜索以查看是否有人首先询问/回答过类似的问题:)
  • 哇,这个解决方案几乎真的拯救了我周末的偏执和沮丧,我在这里遇到了类似的问题,但现在案件已经结案:) stackoverflow.com/questions/46400576/…
【解决方案2】:

根据上面的描述,如果您设置的意图不是字符串,您可以做的另一个技巧是:

Random random = new Random();
intent.setType(String.valueOf(random.nextInt(1000)));

这将调用 onGetViewFactory();

【讨论】:

  • 是的,完美!效果很好。
猜你喜欢
  • 2016-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多