How to find the battery recharge time of android mobile programatically?

时光怂恿深爱的人放手 提交于 2019-12-22 08:14:18

问题


I wanted to know that how can we find how much time android device will take to fully charge its battery.

For example if my battery is 0% charged then how long it will take to charge full i.e. 100%, same if my device is 70% charge so if i connect charger now then how long it will take to full charge my device.


回答1:


You should read the official documentation about this. Basically you'll need to use BatteryManager class to find out current state of battery level.




回答2:


This worked for me:

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

See also: http://mihaifonoage.blogspot.de/2010/02/getting-battery-level-in-android-using.html



来源:https://stackoverflow.com/questions/11098779/how-to-find-the-battery-recharge-time-of-android-mobile-programatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!