Home » How to create a Battery Widget

postheadericon How to create a Battery Widget

This article will describe the basics on how to develop a simple Battery Widget. A Battery Widget displays the current battery level. To receive battery level changes, you need to register your app for the Intent.ACTION_BATTERY_CHANGED. A sample application is provided to show the working.

We start with the basics of creating a widget. We need to provide the widget size in the /res/xml/batterywidgetinfo.xml file. We go for a simple 1x1 widget. The content looks like this:

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

<!--  Copyright CMW Mobile.com, 2012.  -->

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialLayout="@layout/widget"
    android:minWidth="72dip"
    android:minHeight="72dip"
    android:updatePeriodMillis="0"
/>

The widget layout is defined within the /res/layout/widget.xml. We go for a simple TextView to display the current battery level. The content looks like this.

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

<!-- Copyright CMW Mobile.com, 2012. -->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/widgetLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">

<TextView android:id="@+id/battery"
    android:background="@android:color/darker_gray"
    android:contentDescription="@string/batteryTitle"
    android:layout_gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="50dp" />

</LinearLayout>

Within the AndroidManifest.xml file we need to define the widget as a receiver:

     <receiver android:label="@string/batteryTitle" android:name=".BatteryWidgetProvider">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
            <meta-data android:name="android.appwidget.provider"
                android:resource="@xml/batterywidgetinfo" />
        </receiver>

Our main class is the BatteryWidgetProvider class. It needs to extend the android.appwidget.AppWidgetProvider class. The next step is to create a service to receive the updates. The service must be defined in the AndroidManifest.xml as well. We use an inner class of the BatteryWidgetProvider class named BatteryUpdateService as the service class. We override the BatteryWidgetProvider onEnabled method to start the service. The onDisabled method can be used to stop the service after all widgets are removed from the home screen.

     <service android:name=".BatteryWidgetProvider$BatteryUpdateService" android:exported="false">
            <intent-filter>
                <action android:name="com.cmwmobile.android.samples.battery.action.UPDATE" />
            </intent-filter>
        </service>

An IntentFilter class is used to register the intent actions which we want to handle within our application. In our case the Intent.ACTION_BATTERY_CHANGED and our own custom defined ACTION_UPDATE. We use an android.content.BroadcastReceiver to receive the intents. The BatteryUpdateService onCreate method is used to register our BroadcastReceiver to receive the intents. The onDestroy method is used to unregister the BroadcastReceiver again. The battery changed intent contains the battery information. we are interested in. We use the following list of battery attributes:

  • Battery level
  • Battery level scale
  • Plugged information
  • Temperature (in degrees Celsius)
  • Voltage

See BatteryManager for more information about all the available attributes. Finally, within the updateBattery method the TextView is updated with the battery information. And that's it. Have fun and don't forget to like, plus or retweet if you like this blog.

Try the Battery Widget 30 days Trail or the full Battery Widget from Google Play to experiance the full potential.

Attachments:
Download this file (SampleBatteryWidget1.0.0.zip)SampleBatteryWidget1.0.0.zip12 Kb