简体   繁体   English

向应用小部件发送更新广播

[英]Sending an update broadcast to an app widget

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);

But for some reason the onUpdate function is not called!但是由于某种原因,没有调用 onUpdate 函数! Does anyone know what the problem might be?有谁知道问题可能是什么? Thanks.谢谢。

Try the following:请尝试以下操作:

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);
}

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.DayActivitiesAppWidget替换为您的AppWidgetProvider类,这样您就可以更新所有小部件实例,然后您可以通过更新单个小部件实例来追踪问题。

Do you try to to catch your broastcast message in onReceive:您是否尝试在 onReceive 中捕获您的广播消息:

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
    String strAction = intent.getAction();
    if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(strAction)) {
        /* Do update */
    }
}

Otherwise, IMO it is better to define your own Message rather than using ACTION_APPWIDGET_UPDATE to clearly logic of update view of widget, something like that, in 1.Declare intent name in Manifest file:否则,IMO 最好定义您自己的消息,而不是使用 ACTION_APPWIDGET_UPDATE 来清楚地更新小部件视图的逻辑,例如在清单文件中的 1.Declare 意图名称:

<action android:name="com.yourdomain.youapp.SETTING_UPDATE" />

2.Define intent name: 2.定义意图名称:

public static final String SETTING_UPDATE = "com.yourdomain.youapp.SETTING_UPDATE";

3.Handle in onReceive: 3.在onReceive中处理:

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
    String strAction = intent.getAction();
    if (SETTING_UPDATE.equals(strAction)) {
        /* Do update setting */
    }
}

Hope it help ^^希望对你有帮助^^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM