Force Android widget to update

前端 未结 2 1713
死守一世寂寞
死守一世寂寞 2020-12-28 22:24

I respond to a button press on my appwidget in the onreceive method. When the button I pressed, I want to force the widget to call the onupdate method. How do I accomplish t

2条回答
  •  滥情空心
    2020-12-28 23:00

    This is kinda crude, but it works rather well for me, as I've found no directly-implemented way to force an update.

    public class Foo extends AppWidgetManager {
       public static Foo Widget = null;
       public static Context context;
       public static AppWidgetManager AWM;
       public static int IDs[];
    
       public void onUpdate(Context context, AppWidgetManager AWM, int IDs[]) {
          if (null == context) context = Foo.context;
          if (null == AWM) AWM = Foo.AWM;
          if (null == IDs) IDs = Foo.IDs;
    
          Foo.Widget = this;
          Foo.context = context;
          Foo.AWM = AWM;
          Foo.IDs = IDs;
    .......
       }
    }
    

    Now, anywhere I want to force the widget to update, it's as simple as:

    if (null != Foo.Widget) Foo.Widget.onUpdate(null, null, null);
    

提交回复
热议问题