从Activity更新AppWidget的ListView

时间:2014-10-05 10:15:56

标签: android android-listview android-appwidget

我有一个带有ListView的AppWidget。我想在Activity类中添加/删除/更新项目后更新ListView。 我之前尝试过这些:

1)

Intent intent = new Intent(this, MyWidgetProvider.class);
intent.setAction("android.appwidget.action.APPWIDGET_UPDATE");
int[] ids = { R.xml.widget_provider };
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
sendBroadcast(intent);

2)

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
ComponentName thisWidget = new ComponentName(this, MyWidgetProvider.class);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.list);

3)

int widgetIDs[] = AppWidgetManager.getInstance(getApplication()).getAppWidgetIds(new ComponentName(getApplication(), MyWidgetProvider.class));
for (int id : widgetIDs) {
    AppWidgetManager.getInstance(getApplication()).notifyAppWidgetViewDataChanged(id, R.id.list);
}

我没有成功。有时候工作,有时候没有。

现在我的问题是:有没有办法100%保证更新appWidget中的列表视图?

0 个答案:

没有答案