We know that Appwidget can set the update interval by setting the updatetimemillies in Widgetinfo, so what happens when we need to update it in real time? How do I update the contents of the Appwidget display? If you add a new itinerary to your travel application, you want to display the content updates immediately in Appwidget.
How is it?
We know that Widgetprovider is essentially a broadcast receiver, so we can send a broadcast that matches it at the data modification point.
When our intent does not come with any additional data, only the action is defined, the OnReceive method in Widgetprovider is called.
Then we will simply implement the following data in real-time update:
Send broadcast:
publicvoidaddData(Context context){ add....(); updateWidget(context)}//发送广播,使appWidget更新privatevoidupdateWidget(Context context) { new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE); context.sendBroadcast(intent);}
Yourappwidgetprovider Code:
@OverridepublicvoidonReceive(Context context, Intent intent) { super.onReceive(context, intent); final AppWidgetManager mgr = AppWidgetManager.getInstance(context); finalnew ComponentName(context, EventWidgetProvider4x2.class); mgr.notifyAppWidgetViewDataChanged(mgr.getAppWidgetIds(cn), R.id.widget4x2LV);}
I wonder when will call Appwidgetprovider's onupdate?
View Appwidgetprovider part of the source code:
public void onreceive (context context, Intent Intent) {String action = inte Nt.getaction (); if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals (ACTION)) {Bundle extras = Intent.getextras (); if (Extras! = null ) {int [] appwidgetids = Extras.getintarray (Appwidgetmanager.extra_appwidge T_ids); if (Appwidgetids! = null && appwidgetids.length > 0 ) {this . OnUpdate (Context, Appwidge Tmanager.getinstance (context), appwidgetids); }}} ...
This is called only if the intent contains extras and the value of key appwidgetmanager.extra_appwidget_ids is not NULL.
When our broadcast does not contain this extras, OnReceive will call, OnUpdate will not.
Always welcome all the road heroes to express their views, ideas worth sharing ~
Android Learning Record (18)---appwidget display content updated with data changes