shell 执行 adb shell dumpsys battery 命令,得到如下信息:
获取shell命令的返回值内容(以下 new ExeCommand().run("dumpsys battery \n", 3000).getResult();部分
),可以参考这篇文章:https://www.cnblogs.com/lipeineng/p/6078859.html
关键之如何获取scale:100中的数字部分?
可以通过正则表达式:
//获取电量
private String getBatteryLevel() {
String res = "";
String lines = new ExeCommand().run("dumpsys battery \n", 3000).getResult();
String pattern = "scale: (\\d)*";
//创建Pattern对象
Pattern p = Pattern.compile(pattern);
//创建Matcher对象
Matcher m = p.matcher(lines);
if (m.find()) {
res = m.group(0).split(":")[1].trim();
}
return res;
}