Force Android widget to update

前端 未结 2 1712
死守一世寂寞
死守一世寂寞 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条回答
  •  Happy的楠姐
    2020-12-28 22:58

    Widget can't actually respond to clicks because it's not a separate process running. But it can start service to process your command:

    public class TestWidget extends AppWidgetProvider {
      public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
            final int N = appWidgetIds.length;
    
            // Perform this loop procedure for each App Widget that belongs to this provider
            for (int i=0; i

    You should also register the new service in your manifest file:

    
    

    You can find another example of UpdateService implementation in Wiktionary sample in SDK

    And here's another good approach Clickable widgets in android

提交回复
热议问题