将更新广播发送到应用程序小部件 [英] Sending an update broadcast to an app widget

查看:112
本文介绍了将更新广播发送到应用程序小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有配置活动的应用程序小部件,当我单击活动中的确定"按钮时,我想触发该小部件的更新.我写了这段代码:

I have an app widget with a configure activity, and I want to trigger an update to the widget when an OK button in the activity is clicked. I wrote this code:

            Intent initialUpdateIntent=new Intent(AppWidgetManager.
                    ACTION_APPWIDGET_UPDATE);
            initialUpdateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                    widgetID);
            sendBroadcast(initialUpdateIntent);

但是由于某种原因,未调用onUpdate函数!有谁知道可能是什么问题? 谢谢.

But for some reason the onUpdate function is not called! Does anyone know what the problem might be? Thanks.

推荐答案

尝试以下操作:

public static void updateWidgets(Context context) {
    Intent intent = new Intent(context.getApplicationContext(), DayActivitiesAppWidget.class);
    intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
    // Use an array and EXTRA_APPWIDGET_IDS instead of AppWidgetManager.EXTRA_APPWIDGET_ID,
    // since it seems the onUpdate() is only fired on that:
    AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);
    int[] ids = widgetManager.getAppWidgetIds(new ComponentName(context, DayActivitiesAppWidget.class));

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
        widgetManager.notifyAppWidgetViewDataChanged(ids, android.R.id.list);

    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
    context.sendBroadcast(intent);
}

用您的AppWidgetProvider类替换DayActivitiesAppWidget,这样您就可以更新所有小部件实例,然后可以通过更新单个小部件实例来跟踪问题.

Replace DayActivitiesAppWidget with your AppWidgetProvider class this way u update all your widget instances, then you can track down the issue with updating a single widget instance.

这篇关于将更新广播发送到应用程序小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆