Recipe Creating an App Widget

This recipe creates a simple App Widget that displays some text on the home screen. The text is configured to update every second, but note that by default, the Android system forces the minimum update time to be 30 minutes.This helps avoid poorly written widgets from draining the battery. Listing 3.17 implements an AppWidgetProvider, which is a subclass of BroadcastReceiver.The main method to override is the onUpdate() function, which gets called when the system determines it is time to update the widgets.

Listing 3.17 src/com/cookbook/widget_example/SimpleWidgetProviderjava package com.cookbook.simple_widget;

import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.Context; import android.widget.RemoteViews;

public class SimpleWidgetProvider extends AppWidgetProvider { final static int APPWIDGET = 1001; @Override public void onUpdate(Context context,

AppWidgetManager appWidgetManager, int[] appWidgetlds) { super.onUpdate(context, appWidgetManager, appWidgetlds); // Loop through all widgets to display an update final int N = appWidgetlds.length; for (int i=0; i<N; i++) {

int appWidgetId = appWidgetIds[i];

String titlePrefix = "Time since the widget was started:"; updateAppWidget(context, appWidgetManager, appWidgetId, titlePrefix);

static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId, String titlePrefix) {

Long millis = System.currentTimeMillis(); int seconds = (int) (millis / 1000); int minutes = seconds / 60; seconds = seconds % 60;

CharSequence text = titlePrefix;

text += " " + minutes + ":" + String.format("%02d",seconds)); // Construct the RemoteViews object.

RemoteViews views = new RemoteViews(context.getPackageName(),

R.layout.widget_layout); views.setTextViewText(R.id.widget_example_text, text);

// Tell the widget manager appWidgetManager.updateAppWidget(appWidgetId, views);

The XML file describing the detailed information on the widget is shown in Listing 3.18.

It shows the size the widget takes on the home screen and how often it should be updated in milliseconds. (The system minimum is 30 minutes.)

Listing 3.18 src/res/xml/widgetjnfo.xml

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

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="146dp" android:minHeight="72dp" android:updatePeriodMillis="1000" android:initialLayout="@layout/widget_layout">

</appwidget-provider>

The view describing the appearance of the widget is laid out in an XML file, as shown in

Listing 3.19.

Listing 3.19 src/res/layout/widgetjayout.xml

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

<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/widget_example_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ff000000" android:background="#ffffffff"

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook


Post a comment