Android update app widget with ListView after phone reboot



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

  1. Find out phone boot completion using BroadCastReceiver and listening to android.intent.action.BOOT_COMPLETED intent
  2. Then on appwidget provider simply update the appwidget
  3. 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


Let’s create our BroadcastReceiver to listen to phone boot completed broadcast.

public class PhoneBootCompleteReceiver extends BroadcastReceiver {

	public static boolean wasPhoneBootSucessful = false;

	/**
	 * Just changing the boolean value on Phone Boot Complete event one can send
	 * custom broadcast from here as well send these broadcast of appwidget
	 * provider to make necessary updates
	 */
	@Override
	public void onReceive(Context context, Intent intent) {
		if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
			wasPhoneBootSucessful = true;
		}
	}
}


For this PhoneBootCompleteReceiver to work,one must include following on their manifest.xml file.

<!-- Permission to receive boot completed intent or broadcast -->
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<!-- Broadcast receiver that responds when phone successfully boots -->
        <receiver android:name=".PhoneBootCompleteReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>


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.


Finally,you can download the whole source code from my github. Till next time,enjoy coding 😉

Advertisements

One thought on “Android update app widget with ListView after phone reboot

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s