Android Learning Record (18)---appwidget display content updated with data changes

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.