1984

Menu

Daily Archives: August 12, 2013

This tutorial is followup of previous tutorial of mine named Android Showing Remote Image on ImageView of App-Widget ListView. If you look upon the WidgetProvider.java class,I haven’t included any functional statement on onUpdate() method except super.onUpdate(context,appWidgetManager,appWidgetIds).
I have stated earlier that onUpdate() method is called on every update interval that is set on appwidget-provider xml file( in our case xml/widgetinfo.xml) and on every phone bootup. So, in our case as there is no functional statement on onUpdate() method, our widget will be blank after phone reboot or on phone start.If one includes,update logic on onUpdate() method of appwidget,then there is no need to care about phone reboot. But in our case we need to address phone reboot/start issue as our appwidget will be blank after phone reboot/start.
I will first point out the logic,I am going to implement on this tutorial

Find out phone boot completion using BroadCastReceiver and listening to android.intent.action.BOOT_COMPLETED intent

Then on appwidget provider simply update the appwidget

I am just using a boolean value,but one can send custom broadcast to appwidget provider,notifying about phone boot completion and making necessary updates out there

Now moving on the our appwidgetprovide class i.e.WidgetProvider,following changes are to be made

/*
* this method is called every 30 mins as specified on widgetinfo.xml this
* method is also called on every phone reboot so on phone reboot,we need to
* once again update the widget i.e populate the listview of the widget
*/
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
if (PhoneBootCompleteReceiver.wasPhoneBootSucessful) {
PhoneBootCompleteReceiver.wasPhoneBootSucessful = false;
final int N = appWidgetIds.length;
for (int i = 0; i < N; i++) {
RemoteViews remoteViews = updateWidgetListView(context,
appWidgetIds[i]);
appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
}
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}

This is all one has to do, in order to update appwidget on phone startup. All these tutorials of AppWidget ListView is to show the basics of appwidget containing ListView,along with procedure to fill up data from remote or web.

One important thing I want to mention is that, while implementing own update interval i.e. not relying upon update interval set at appwidget-provider.xml,one has to use appWidgetManager.notifyAppWidgetViewDataChanged() instead of appWidgetManager.updateAppWidget(). This alone doesn’t change the data of the ListView.This process calls onDataSetChanged() of RemoteViewsFactory. So at onDataSetChanged,you have to again fetch data that is supplied to RemoteViewsFactory. Then only,the changed data is reflected upon appwidget ListView.