title: How to get current battery percentage?
body: <p>I'm creating a battery monitor application, and I'm trying to display the current battery percentage.  I have a broadcastreceiver, that is listening for...</p>
  
  <pre><code>&lt;action android:name="android.intent.action.BATTERY_CHANGED" /&gt;
  </code></pre>
  
  <p>In the receiver I have the following code.</p>
  
  <pre><code>int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
  int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
  
  currentBatteryLevel = level / (float) scale;
  
  Toast.makeText(context, "Current battery level: " + Float.toString(currentBatteryLevel) + "%", Toast.LENGTH_SHORT).show();
  </code></pre>
  
  <p>For some reason, the result is always <code>Current battery level: 1.0%</code>...Where am I going wrong?</p>
  
  <p>Edit:</p>
  
  <p>Reciever is registered in the manifest as...</p>
  
  <pre><code>&lt;receiver android:name=".PowerMonitorReceiver"&gt;
      &lt;intent-filter&gt;
          &lt;action android:name="android.intent.action.ACTION_POWER_CONNECTED" /&gt;
          &lt;action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" /&gt;
          &lt;action android:name="android.intent.action.BATTERY_LOW" /&gt;
          &lt;action android:name="android.intent.action.BATTERY_OKAY" /&gt;
          &lt;action android:name="android.intent.action.BATTERY_CHANGED" /&gt;
      &lt;/intent-filter&gt;
  &lt;/receiver&gt;
  </code></pre>
  
  <p>and my receiver is...</p>
  
  <p>package com.garciaericn.t2d;</p>
  
  <pre><code>import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.os.BatteryManager;
  import android.util.Log;
  import android.widget.Toast;
  
  public class PowerMonitorReceiver extends BroadcastReceiver {
  
      private static final String TAG = "PowerMonitorReceiver.TAG";
      private float currentBatteryLevel;
  
      @Override
      public void onReceive(Context context, Intent intent) {
  
  
  
          // Obtain action from intent to check which broadcast is being received
          String action = intent.getAction();
  
          // Perform action according to type
          switch (action) {
              case (Intent.ACTION_BATTERY_CHANGED): {
                  Log.i(TAG, "Battery has changed");
  
                  int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
                  int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
  
                  currentBatteryLevel = (level * 100) / (float) scale;
  
                  Toast.makeText(context, "Current battery level: " + Float.toString(currentBatteryLevel) + "%" + " level: " + level + " scale: " + scale, Toast.LENGTH_LONG).show();
  
                  break;
              }
              case (Intent.ACTION_POWER_CONNECTED): {
                  Log.i(TAG, "Power connected");
  
                  int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
                  boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
  
                  int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
                  boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
                  boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
  
                  String chargingType = null;
  
                  if (usbCharge) {
                      chargingType = "USB";
                  } else if (acCharge) {
                      chargingType = "AC Power";
                  }
  
                  if (isCharging &amp;&amp; chargingType != null) {
                      Toast.makeText(context, "Device is charging via: " + chargingType, Toast.LENGTH_SHORT).show();
                  } else {
                      Toast.makeText(context, "Device is charging.", Toast.LENGTH_SHORT).show();
                  }
                  break;
              }
              case (Intent.ACTION_POWER_DISCONNECTED): {
                  Log.i(TAG, "Power disconnected");
                  Toast.makeText(context, "Power Disconnected", Toast.LENGTH_SHORT).show();
                  break;
              }
              case (Intent.ACTION_BATTERY_LOW): {
                  Log.i(TAG, "Battery low");
                  Toast.makeText(context, "Battery low", Toast.LENGTH_SHORT).show();
                  break;
              }
              case (Intent.ACTION_BATTERY_OKAY): {
                  Log.i(TAG, "Battery Okay");
                  Toast.makeText(context, "Battery Okay", Toast.LENGTH_SHORT).show();
                  break;
              }
          }
      }
  }
  </code></pre>