Sunday, February 19, 2012

Android Sample Code: Battery Status

Here I want to share Battery Status Application that output as below:

Before I share my source code for Battery Status Application for Android. Below are the tools that you need to develop an Android Application or Widget:
  1. Android Development Environment - eclipse
  2. Android SDK
  3. Android Virtual Device Manager
*For details on how to do the installation, you may refer to Android Developers page.

For this Battery Status Application, below is the general outline:
  • The layout is using ListView as the main and TextView for the individual row.
  • Upon starting the application, a thread will be created to pull the battery info in 1-second interval. The battery info is being pulled using the BroadcastReceiver.

Below are the code for layout file "list_item.xml":
 <?xml version="1.0" encoding="utf-8"?>  
             android:clickable="false" />  

Below are the code for the main class:
 public class BatteryStatusActivity extends ListActivity {  
   /** Called when the activity is first created. */  
      private String[] strText = new String[] {"Battery Level", "Voltage", "Status"};  
      private int voltage = 0;  
      private boolean trun = true;  
      private Handler myHandler = new Handler();  
      private Runnable myRun = new Runnable() {  
           public void run() {  
      // using Thread to keep the process running  
      private Thread myThread = new Thread() {  
           public void run () {  
                do {  
                  try {  
                     } catch (InterruptedException e) {  
                          // TODO Auto-generated catch block  
                } while (trun);  
   public void onCreate(Bundle savedInstanceState) {  
     Toast.makeText(getApplicationContext(), "email", 2000).show();  
   public void onDestroy() {  
        trun = false;  

   private void startMonitor() {  
   private void updateNow() {  
        ListView thisListView = getListView();  
        ArrayAdapter<String> myList = new ArrayAdapter<String>(this, R.layout.list_item, strText);  
   private void batteryLevelUpdate() {  
     BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {  
       public void onReceive(Context context, Intent intent) {  
         int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);  
         int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);  
         int level = -1;  
         if (rawlevel >= 0 && scale > 0) {  
           level = (rawlevel * 100) / scale;  
         voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1);  
         int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);  
         int onplug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);  
         boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;  
         boolean onUSB = onplug == BatteryManager.BATTERY_PLUGGED_USB;  
         boolean onAC = onplug == BatteryManager.BATTERY_PLUGGED_AC;  
         String strStatus = "Charging on ";  
         if (isCharging && onUSB)  
              strStatus += "USB";  
         else if (isCharging && onAC)  
              strStatus += "AC Power";  
              strStatus = "Battery Discharging";  
         strText[0] = "Battery Level: " + Integer.toString(level) + "%";  
         strText[1] = "Voltage: " + Integer.toString(voltage) + "mV";  
         strText[2] = strStatus;  
     IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);  
     registerReceiver(batteryLevelReceiver, batteryLevelFilter);  

Take note that:
If you want the ListView is non-clickable & non-scrollable, you need to set the following:
 ListView thisListView = getListView();  

If you want the ListView is non-clickable but still scrollable, you need to set the following:
 ArrayAdapter<String> myList = new ArrayAdapter<String>(this, R.layout.list_item, strText) {  
       public boolean isEnabled(int position)  
           return false;  

That's all for now.
*How did I place the source code here? visit Code Formatter

You may download full source code here: Android Apps

:: aerobrainTech ::


  1. Android application development affiliation is vested with a huge commitment to make the best applications for its customers that can offer them some assistance with improving their bit of the general business and access clients. Along these lines, we at Webnet, ensure that we go on for you the applications that are exceedingly profitable in the business part and somewhat by guarantee you move among your foes.

  2. Very helpful suggestions that help in the optimizing website. Thank you for valuable suggestions.
    aIperius Backup 4.7.1