4

I've been searching for this and only find really messed up things. Isn't there a easy way to show the battery level like 21% on a toast or Textview? Or how can i achieve this?

//Simon

jimmym715
  • 1,514
  • 1
  • 16
  • 25
carefacerz
  • 1,217
  • 3
  • 15
  • 27
  • possible duplicate of [Android Battery in SDK](http://stackoverflow.com/questions/1805895/android-battery-in-sdk) – Ted Hopp Jul 03 '11 at 17:52
  • If you want to read it from the file system you can get that from /sys/class/power_supply/battery/capacity which by default is 50. – JPM Aug 09 '12 at 16:48

5 Answers5

14

To get the battery level right now, call:

registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

(note: typing that in from memory, please adjust if needed)

This will return an Intent with various extras documented on the BatteryManager class. Use BatteryManager.EXTRA_LEVEL and BatteryManager.EXTRA_SCALE to determine the percentage remaining.

If you need to know over a period of time as the battery changes, use the BroadcastReceiver approach @Augusto outlined in his answer.

CommonsWare
  • 910,778
  • 176
  • 2,215
  • 2,253
10

if you mean changing the battery status on the emulator do the following. Connect to the emulator via telnet and change the status and capacity

> telnet localhost 5554
Android Console: type 'help' for a list of commands
OK
power ac off
OK
power discharging
OK
power capacity 21
OK
exit
>

Ahh, well you can do what it says on the page Ted mentioned, and then use a handler to show a toast. This is the code you need to add to your activity.

private Handler handler = new Handler;

private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(final Context context, Intent intent) {
        int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
        int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
        Log.i(TAG, "level: " + level + "; scale: " + scale);
        int percent = (level*100)/scale;

        final String text = String.valueOf(percent) + "%";
        handler.post( new Runnable() {

            public void run() {
                Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
            }
        });

    }
};
Augusto
  • 26,525
  • 5
  • 52
  • 82
  • Sorry, i mean like BatteyManager.getLevel() but there isnt. so basicly just show how much battery procentage i have on my phone – carefacerz Jul 03 '11 at 17:41
  • 3
    @Augusto: Ideally, use `BatteryManager.EXTRA_LEVEL` instead of `"level"`. Also, the level is not a percentage. It is a value ranging from 0 to the value of the `BatteryManager.EXTRA_SCALE` extra. On many devices, the scale *is* 100, but do not assume that. – CommonsWare Jul 03 '11 at 18:31
  • Don't forget to register the receiver: this.registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); :) – Victor Mar 11 '14 at 21:04
6
    public static String batteryLevel(Context context)
    {
        Intent intent  = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));   
        int    level   = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
        int    scale   = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
        int    percent = (level*100)/scale;
        return String.valueOf(percent) + "%";
    }
XXX
  • 8,662
  • 7
  • 42
  • 52
0

You can use this.

private void batteryLevel() {
        BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                //context.unregisterReceiver(this);
                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;
                }
                batterLevel.setText("Battery Level Remaining: " + level + "%");
            }
        };
        IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        registerReceiver(batteryLevelReceiver, batteryLevelFilter);
    }
reza.cse08
  • 5,172
  • 38
  • 34
0

I used a combination like that:

 BatteryManager bm = (BatteryManager) getActivity().getSystemService(BATTERY_SERVICE);
 int batLevel = 100;
 int level;
 int scale;
 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
      batLevel = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
 }
 if(batLevel == 0){
     Intent intent  = getActivity().registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
     level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
     scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
     batLevel = (level*100)/scale;
}

I did it like that because on a Nexus 5 device with Marshmallow "batLevel" returned 0 always. That solved it completely!

Good luck

Idan Damri
  • 156
  • 8