android appwidget won't update from activity

前端 未结 2 1634
慢半拍i
慢半拍i 2020-12-16 01:54

I have a simple appwidget and I want to update it when an action occurs in an activity (in the same app). in onUpdate(), I immediately update the widget, which works fine. I

2条回答
  •  清歌不尽
    2020-12-16 02:23

    Without seeing your code I'm not 100% sure how you are trying to do it, however here is the method I use. Within my Activity I have the following method:

    private void updateAllWidgets(){
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
        int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(this, MyWidget.class));
        if (appWidgetIds.length > 0) {
            new MyWidget().onUpdate(this, appWidgetManager, appWidgetIds);
        }
    }
    

    Where MyWidget is the class of the appwidget. I can call this method from anywhere within my Activity to update all my appwidgets.

提交回复
热议问题