android – onGetViewFactory只为多个小部件调用一次

前端之家收集整理的这篇文章主要介绍了android – onGetViewFactory只为多个小部件调用一次前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个使用ListView的appwidget.
我创建了一个扩展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方法只能被调用一次.这导致所有的小部件显示相同的数据.

如何解决这个问题?

解决方法

我没有测试过这个,但是看到RemoteViewsService.onBind()的源代码,我相信你不能只是改变你的Intent中的extras,以便它检测到对onGetViewFactory()方法的一个新的调用是因为它使用Intent.filterEquals()方法进行比较:

Determine if two intents are the same for the purposes of intent resolution (filtering). That is,if their action,data,type,class,and categories are the same. This does not compare any extra data included in the intents.

我建议通过Intent的数据传递你需要的信息(widget id?),也可以这样:

private static void fillInList(RemoteViews widget,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);
}
原文链接:https://www.f2er.com/android/312850.html

猜你在找的Android相关文章