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.
Add the corresponding service> tag to our AndroidManifest.xml file
What are the next steps?
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:
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"; @Override protected void onUpdateData(int reason) { IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); Intent batteryStatus = getApplicationContext().registerReceiver(null, ifilter); // Level int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); //health //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) charging=getString(R.string.full); 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(R.string.mv); // Publish the extension data update. publishUpdate(new ExtensionData() .visible(true) .icon(R.drawable.ic_extension_battery) .status("" + level + "%") .expandedTitle("" + level + "% " + charging) .expandedBody( charge + " - " + temperature/10 + umTemp+" - " + voltage+umMv)); } }We first retrieve information using
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); Intent batteryStatus = getApplicationContext().registerReceiver(null, ifilter);and then publish our ExtensionData with
publishUpdate()
.Add the corresponding service> tag to our AndroidManifest.xml file
And that's all....
What are the next steps?
- 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..
- Add a listener to monitor changes in charging state
- Calculate remaining time
- ......
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:
the two screen shots where you add the extension, where is that accesses from?
ReplyDeleteok got it, needed to install dashclock widget first :$
DeleteIf Your are not ediot then try this app Pixelo Mod Apk
ReplyDelete