小部件列表视图未使用 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 进行更新的主要内容,如果未能解决你的问题,请参考以下文章

OnUpdate() 未调用小部件服务

Android 无法使用列表视图加载主屏幕小部件

Android 小部件 - 使用带有远程视图的 setOnClickPendingIntent 未收到 Intent

如何将列表视图与其他小部件一起使用?

小部件未出现在小部件列表中

毕加索:将图像加载到小部件列表视图