小部件列表视图未使用 notifyAppWidgetViewDataChanged 进行更新
Posted
技术标签:
【中文标题】小部件列表视图未使用 notifyAppWidgetViewDataChanged 进行更新【英文标题】:Widget Listview is not updating with notifyAppWidgetViewDataChanged 【发布时间】:2015-06-18 17:29:57 【问题描述】:我是 android 开发的新手。我正在尝试使用 Web 服务在我的小部件中加载列表视图。它加载完美,但我想根据我的要求动态更新我的列表视图。
这里附上我的代码:
package com.widget;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.util.Log;
import android.widget.RemoteViews;
public class WidgetProvider extends AppWidgetProvider
public static final String DATA_FETCHED = "com.widget.DATA_FETCHED";
public static String UPDATE_LIST = "UPDATE_LIST";
AppWidgetManager appWidgetManager;
ComponentName currentWidget;
public static Integer randomNumber;
SharedPreferences sharedprefer;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds)
final int N = appWidgetIds.length;
for (int i = 0; i < N; i++)
Intent serviceIntent = new Intent(context, RemoteFetchService.class);
serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
appWidgetIds[i]);
context.startService(serviceIntent);
// appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds[i],
// R.id.listViewWidget);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds[i], R.id.listViewWidget);
super.onUpdate(context, appWidgetManager, appWidgetIds);
@Override
public void onReceive(Context context, Intent intent)
super.onReceive(context, intent);
if (intent.getAction().equals(DATA_FETCHED))
int appWidgetId = intent.getIntExtra(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
AppWidgetManager appWidgetManager = AppWidgetManager
.getInstance(context);
RemoteViews remoteViews = updateWidgetListView(context, appWidgetId);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId,
R.id.listViewWidget);
private RemoteViews updateWidgetListView(Context context, int appWidgetId)
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
Intent svcIntent = new Intent(context, WidgetService.class);
svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
remoteViews.setRemoteAdapter(appWidgetId, R.id.listViewWidget,
svcIntent);
remoteViews.setEmptyView(R.id.listViewWidget, R.id.empty_view);
updateWidget(context, remoteViews);
return remoteViews;
public static void updateWidget(Context context, RemoteViews remoteViews)
AppWidgetManager appWidgetManager = AppWidgetManager
.getInstance(context);
int appWidgetIds[] = appWidgetManager
.getAppWidgetIds(new ComponentName(context,
WidgetProvider.class));
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds,
R.id.listViewWidget);
【问题讨论】:
【参考方案1】:在onReceive
改变你的
if (intent.getAction().equals(DATA_FETCHED))
到
if (intent.getAction().equals(AppWidgetManager.ACTION_APPWIDGET_UPDATE))
它应该可以工作。
如果还是不行,把上面if
的内容替换成:
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName thisAppWidget = new ComponentName(context.getPackageName(), Your_Widget_Class_Here.class.getName());
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds,R.id.listView);
【讨论】:
【参考方案2】:在 Android 中经常使用postDelayed:
new Handler().postDelayed(new Runnable()
@Override
public void run()
AppWidgetManager.getInstance(context).notifyAppWidgetViewDataChanged(appWidgetIds, R.id.listview);
, 1000);
【讨论】:
以上是关于小部件列表视图未使用 notifyAppWidgetViewDataChanged 进行更新的主要内容,如果未能解决你的问题,请参考以下文章