Created
November 17, 2011 16:24
-
-
Save sergeich/1373601 to your computer and use it in GitHub Desktop.
Undocumented method of getting battery level in Android. Not recommended, but it can get battery level immediately.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static final String BATTERY_CAPACITY_SYSFS = "/sys/class/power_supply/battery/capacity"; | |
private static String readSysFsFile(String a_File) throws Exception { | |
FileReader a_Reader = new FileReader(a_File); | |
int a_Char; | |
String a_Content = ""; | |
while ((a_Char = a_Reader.read()) != -1) { | |
// Ignore \n | |
if (a_Char != 10) { | |
a_Content += Character.toString((char) a_Char); | |
} | |
} | |
return a_Content; | |
} | |
public static String getBatteryStatus() { | |
try { | |
String a_Capacity = readSysFsFile(BATTERY_CAPACITY_SYSFS); | |
if (a_Capacity.length() == 0) { | |
a_Capacity = "Error"; | |
} | |
return a_Capacity; | |
} catch (Exception a_Ex) { | |
return "Error"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment