android appwidget won't update from activity

前端 未结 2 1625
慢半拍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:25

    Rather than use a static method take advantage of the fact that the widget is already a broadcast receiver and register an update intent for it in your manifest. Then whenever you need to update it from the activity just call

    //in your activity
    sendBroadcast(new Intent(MyWidget.ACTION_UPDATE).putExtra(whatever));  
    
    
    
    //In widget class
    public static final String ACTION_UPDATE = "com.example.UPDATE_MY_WIDGET";
    

    and inside the receiver tags of your manifest file

    
      
    
    

提交回复
热议问题