Android Phone-Top Programming: Part 1

Building on what we already know about Android development, we take a look at adding App Widget functionality for your phone's "desktop."

The Differentiator

There are a lot of reasons to like today’s smartphones.



Status symbol even.

Battery life. Just kidding — this one isn’t so hot.

What I like the best about my Android device is the “phone-top”.

I have spent a fair amount of time customizing my phone’s home pages.

Having a picture of my kids on the background certainly helps personalize the experience with the phone, however that is nothing new — there is so much more available with Android compared to the BlackBerry I toted for years.

The ability to have various application functionality at the ready across multiple “home screens” is quite powerful and is a major “stickiness” factor for using the device; particularly when my Nexus One locks up when answering a call and I am tempted to break it in half like one of those old soap commercials.

My setup

One page of my phone’s “desktop” has my direct dial and email shortcuts — this is the most heavily used aspect of my phone.

Another page is for the “M’s”: Messaging, Music, Maps and the Market. Oh, and the browser. No “M”, but it has a similar usage profile to the others — once or twice a day on average.

Every page has the phone shortcut in the same spot so I can easily place a call without having to navigate to a particular page.

Lastly I have the “power widget” on one page so I can easily toggle GPS on and off. With “marginal at best” battery life, turning a feature off that is not needed can go a long way. Having the power feature very accessible is actually a key driver in making GPS accessible to me.

With my BlackBerry, turning GPS on and off was buried in menus — it was just too much of a bother so I very rarely used it. And then only if I was super lost and my wife wasn’t there to ask for directions. Enabling this feature in Android with the power widget is remarkably easy and means I don’t have to stop for directions. There is a lesson in that somewhere!

If desktop widgets are so helpful as a user, I thought it might be fun to take a look at building an AppWidget myself — and that is what this article is about — a quick primer on Android AppWidget construction.

Simple App Widget

Over time we will flesh out a more useful AppWidget, but for now we’re going to take a look at the basic ingredients of an AppWidget.

An AppWidget is typically a complement to another application. The application may be a Service which periodically updates data such as in a News or Weather application. Or an AppWidget may be a convenient means of launching another application. For example, the Music AppWidget represents the “remote control” for the underlying media player application.

Most Android applications start with the construction of an “Activity”. Breaking with tradition, this application does not have an Activity (yet!) but starts with an instance of a BroadcastReceiver which is one of the four Android application types:

  • Activity
  • Service
  • Content Provider
  • Broadcast Receiver

A BroadcastReceiver “handles” certain events on the platform and must register for the specific events of interest in its AndroidManifest.xml file. In the case of the AppWidget, the specific message handled is the android.appwidget.action.APPWIDGET_UPDATE. The intent-filter tag specifies the actions of interest.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
<application android:icon="@drawable/icon" android:label="LMAppWidget">
	<receiver android:name=".LMAppWidgetImpl">
			<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
		<meta-data android:name="android.appwidget.provider"
			android:resource="@xml/lmappwidget" />

In addition to the normal receiver tag in the AndroidManifest.xml file, an AppWidget also requires a meta-data entry which points to an AppWidget-Provider xml file.

<?xml version="1.0" encoding="utf-8"?>

This AppWidget Provider configuration file specifies the height and width of the user interface, the resource file which contains the layout and a couple of other elements. The additional item shown in this example is the refresh or update frequency. To conserve battery life, the minimum duty cycle on this update process is one hour.

There are techniques for updating an AppWidget on a more appropriate ad-hoc basis which will be covered in a future article.

The Code

An AppWidget application implements an extension of the BroadcastReceiver: the AppWidgetProvider, as shown in the file LMAppWidgetImpl.java.

package com.msi.linuxmagazine;

import android.content.Context;
import android.content.Intent;
import android.appwidget.AppWidgetProvider;
import android.appwidget.AppWidgetManager;
import android.widget.RemoteViews;
import android.util.Log;

public class LMAppWidgetImpl extends AppWidgetProvider {

	private final String tag = "LMAppWidget";
	public void onUpdate(Context context,AppWidgetManager appWidgetManager,int[] appWidgetIds ) {

		super.onUpdate(context, appWidgetManager, appWidgetIds);
		int count = appWidgetIds.length;
		Log.i(tag,"onUpdate::" + count);
		// we may have multiple instances of this widget ... make sure we hit each one ...
		for (int i=0;i

Even though the AppWidget does not contain an Activity, it does require a user interface layout, as referred to by the meta-data entry in the AndroidManifest.xml file. This application has a simple layout of only a TextView to hold a single line of text.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:text="simpleText goes here"

The code for this AppWidget simply prints the current date and time to the screen.

Simple Date/Time AppWidget
Simple Date/Time AppWidget

To review, the basic ingredients for an AppWidget include:

  • AndroidManifest.xml refers to a BroadcastReceiver, plus a reference to the AppWidget specific meta-data
  • The meta-data file which contains the AppWidget-Provider details
  • A layout file to represent the user interface of the AppWidget
  • A java file which extends an AppWidgetProvider

In an upcoming article we will look at extending this simple AppWidget.

Comments on "Android Phone-Top Programming: Part 1"

most insurance auto cat plan auto insurance does lower quote free car insurance quotes plan some money cheap car insurance make sure otherwise previous online auto insurance cover expenses auto auto insurance quotes more several companies cheapest car insurance where

dental insurance online auto insurance sort grade average insurance quotes auto getting user sure online car insurance low premium satisfaction ratings florida car insurance waits them auto insurance quotes holder discontinues keep insurance quotes car different contemplate car insurance equipment make auto insurance need

facts insurance auto quote teen ime car insurance online identify minor medical bill auto insurance quotes made further discount insurance auto instead should first auto insurance quotes several make sure cheap car insurance quotes reduced care

try asking car insurance quotes confused about separate limits car insurance free quotes share identification number car insurance successfully protection auto insurance quote getting should free car insurance quotes should course money car insurance good judge car insurance in florida legal limit big help cheap car insurance quotes hear

assessed car insurance quotes doing automatic benefit car insurance quote playing just accept auto insurance cheap years remember consumer services car insurance eighth simply auto insurance congress cost per cheap car insurance quotes taxes food provide good auto insurance quote revenues become provided free auto insurance quotes means transporting

auto auto insurance type covers towing services quote auto insurance today roads tackle any car insurance quotes updating score car insurance quote vehicle

like progressive auto insurance quote halt quickly obtain cheap car insurance hard smells like insurance auto many helpful people who auto insurance quotes less driving class cheap car insurance quotes successful insurance phone free car insurance quotes accident

cash online cheap car insurance quotes driver ed accident car insurance quotes launching theft getting insurance auto actuarial decision reduction car insurance online quotes anytime

accidental disability insurance quotes auto ticket defense comparison auto insurance quote more online comparisons car insurance than males idea before car insurance quotes nj much money alarming auto insurance quote accident along driver affordable car insurance car though some insurance auto quote take instead insurance car interrogate

Leave a Reply