开发者

Android Development: Show Battery Level

开发者 https://www.devze.com 2023-03-16 20:41 出处:网络
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?

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? O开发者_JAVA百科r how can i achieve this?

//Simon


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.


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();
            }
        });

    }
};


    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) + "%";
    }


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);
    }


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

0

精彩评论

暂无评论...
验证码 换一张
取 消