从活动更新多个主屏幕小部件

时间:2013-05-10 07:59:42

标签: android android-layout android-widget

我的主屏幕上有两个小部件。我知道如何从我的Main活动更新单个小部件。当我按下主要活动上的后退按钮时,以下代码会更新我的单个小部件。

@Override
public void onBackPressed() {
    Intent intent = new Intent(this, todo_widget.class);
    intent.setAction("android.appwidget.action.APPWIDGET_UPDATE");
    int[] ids = AppWidgetManager.getInstance(getApplication())
            .getAppWidgetIds(
                    new ComponentName(getApplication(),         todo_widget.class));
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
    sendBroadcast(intent);}

但如果我把我的代码放在这样的小部件中,这两个小部件都没有更新: -

@Override
public void onBackPressed() {
    Intent intent = new Intent(this, todo_widget.class);
    intent.setAction("android.appwidget.action.APPWIDGET_UPDATE");
    int[] ids = AppWidgetManager.getInstance(getApplication())
            .getAppWidgetIds(
                    new ComponentName(getApplication(),     todo_widget.class));
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
    sendBroadcast(intent);
    Intent intent1 = new Intent(this, todo_widget2.class);
    intent1.setAction("android.appwidget.action.APPWIDGET_UPDATE");
    int[] ids1 = AppWidgetManager.getInstance(getApplication())
            .getAppWidgetIds(
                    new ComponentName(getApplication(), todo_widget2.class));
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids1);
    sendBroadcast(intent1);
    this.finish();
}

请有人帮我解决这个问题。当我按下后退按钮或创建主要活动时,如何更新小部件?

0 个答案:

没有答案