Google 源码的电池百分比获取和转换
package com.android.settingslib;
public class Utils {
/** Formats a double from 0.0..100.0 with an option to round **/
public static String formatPercentage(double percentage, boolean round) {
final int localPercentage = round ? Math.round((float) percentage) : (int) percentage;
return formatPercentage(localPercentage);
}
/** Formats the ratio of amount/total as a percentage. */
public static String formatPercentage(long amount, long total) {
return formatPercentage(((double) amount) / total);
}
/** Formats an integer from 0..100 as a percentage. */
public static String formatPercentage(int percentage) {
return formatPercentage(((double) percentage) / 100.0);
}
/** Formats a double from 0.0..1.0 as a percentage. */
public static String formatPercentage(double percentage) {
return NumberFormat.getPercentInstance().format(percentage);
}
public static int getBatteryLevel(Intent batteryChangedIntent) {
int level = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
int scale = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
return (level * 100) / scale;
}