How to write a DashClock Extension: Battery Extension example

With DashClock API it is very easy to write a DashClock Extension.

UPDATE 27/05/2013---------------------------------------------------.
You can find it in Google Play.

In this github repository you can find a full version.

Here you can find a very simple Battery Extension.
This is just an example (bit raw) that should be improved !!.

We are going to write our service BatteryExtension that extends the DashClockExtension class.
public class BatteryExtension extends DashClockExtension {

 private static final String TAG = "BatteryExtension";

 protected void onUpdateData(int reason) {

  IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
  Intent batteryStatus = getApplicationContext().registerReceiver(null,

  // Level
  int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);

  //int health = batteryStatus.getIntExtra(BatteryManager.EXTRA_HEALTH, -1);
  // How are we charging?
  int chargePlug = batteryStatus.getIntExtra(
    BatteryManager.EXTRA_PLUGGED, -1);
  boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
  boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
  boolean wirelessCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_WIRELESS;

  String charge = "";
  if (usbCharge)
   charge = getString(R.string.charge_usb);
  else if (acCharge)
   charge = getString(R.string.charge_ac);
  else if (wirelessCharge)
   charge = getString(R.string.charge_wireless);

  // Are we charging / charged?
  int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
  boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING;
  boolean isFull = status == BatteryManager.BATTERY_STATUS_FULL;
  //boolean isDischarging = status == BatteryManager.BATTERY_STATUS_DISCHARGING;

  String charging = getString(R.string.discharging);
  if (isFull)
  else if (isCharging)
   charging = getString(R.string.charging);
  //String technology = batteryStatus.getExtras().getString(BatteryManager.EXTRA_TECHNOLOGY);
  int temperature = batteryStatus.getIntExtra(
    BatteryManager.EXTRA_TEMPERATURE, 0);
  int voltage = batteryStatus
    .getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0);

  String umTemp=getString(R.string.celsius);
  String umMv=getString(;
  // Publish the extension data update.
  publishUpdate(new ExtensionData()
    .status("" + level + "%")
    .expandedTitle("" + level + "% " + charging)
      charge +  " - " + temperature/10 + umTemp+" - "
        + voltage+umMv));


We first retrieve information using
     IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
     Intent batteryStatus = getApplicationContext().registerReceiver(null,
and then publish our ExtensionData with publishUpdate().
Add the corresponding service> tag to our AndroidManifest.xml file

            android:permission="" >
                <action android:name="" />

                android:value="1" />
                android:value="@string/extension_description" />
And that's all....

What are the next steps?
  1. Add a settings activity for example to choose frequency update.Actually onUpdateData(int) will by default be called roughly once per hour, to select the unit of measurement for temperature..
  2. Add a listener to monitor changes in charging state
  3. Calculate remaining time
  4. ......

UPDATE: I've improved code above.
In github repository you can find updated code.

UPDATE 2: In this github repository you can find a full version.

In the next post I'll try to write a Dial Extension.

You can get code from GitHub:


  1. the two screen shots where you add the extension, where is that accesses from?

    1. ok got it, needed to install dashclock widget first :$


Post a Comment

Popular posts from this blog

AntiPattern: freezing a UI with Broadcast Receiver

How to centralize the support libraries dependencies in gradle

NotificationListenerService and kitkat