完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
就是从文件中读值,由于数据变化太快,显示是连续读5次的数据,直接上代码:
public class MainActivity extends Activity { private boolean mIsStart = true; private Toast mToast; private Handler mHandler = new Handler() { public void handleMessage(android.os.Message msg) { showToast(getCurrent()); if (mIsStart) { // 因为Toast.LENGTH_SHORT的默认值是2000 mHandler.sendEmptyMessageDelayed(0, 1900); } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mHandler.sendEmptyMessage(0); } @Override protected void onDestroy() { super.onDestroy(); mHandler.removeMessages(0); mIsStart = false; } /** * 获取当前电流 */ private String getCurrent() { String result = "null"; try { Class systemProperties = Class.forName("android.os.SystemProperties"); Method get = systemProperties.getDeclaredMethod("get", String.class); String platName = (String) get.invoke(null, "ro.hardware"); if (platName.startsWith("mt") || platName.startsWith("MT")) { String filePath = "/sys/class/power_supply/battery/device/FG_Battery_CurrentConsumption"; // MTK平台该值不区分充放电,都为负数,要想实现充放电电流增加广播监听充电状态即可 result = "当前电流为:" + Math.round(getMeanCurrentVal(filePath, 5, 0) / 10.0f) + "mA"; result += ", 电压为:" + readFile("/sys/class/power_supply/battery/batt_vol", 0) + "mV"; } else if (platName.startsWith("qcom")) { String filePath ="/sys/class/power_supply/battery/current_now"; int current = Math.round(getMeanCurrentVal(filePath, 5, 0) / 10.0f); int voltage = readFile("/sys/class/power_supply/battery/voltage_now", 0) / 1000; // 高通平台该值小于0时电池处于放电状态,大于0时处于充电状态 if (current < 0) { result = "充电电流为:" + (-current) + "mA, 电压为:" + voltage + "mV"; } else { result = "放电电流为:" + current + "mA, 电压为:" + voltage + "mV"; } } } catch (Exception e) { e.printStackTrace(); } return result; } /** * 获取平均电流值 * 获取 filePath 文件 totalCount 次数的平均值,每次采样间隔 intervalMs 时间 */ private float getMeanCurrentVal(String filePath, int totalCount, int intervalMs) { float meanVal = 0.0f; if (totalCount <= 0) { return 0.0f; } for (int i = 0; i < totalCount; i++) { try { float f = Float.valueOf(readFile(filePath, 0)); meanVal += f / totalCount; } catch (Exception e) { e.printStackTrace(); } if (intervalMs <= 0) { continue; } try { Thread.sleep(intervalMs); } catch (Exception e) { e.printStackTrace(); } } return meanVal; } private int readFile(String path, int defaultValue) { try { BufferedReader bufferedReader = new BufferedReader(new FileReader( path)); int i = Integer.parseInt(bufferedReader.readLine(), 10); bufferedReader.close(); return i; } catch (Exception localException) { } return defaultValue; } private void showToast(String content) { if (mToast == null) { mToast = Toast.makeText(MainActivity.this, content, Toast.LENGTH_SHORT); } else { mToast.setText(content); mToast.setDuration(Toast.LENGTH_SHORT); } mToast.show(); }} |
|
|
|
只有小组成员才能发言,加入小组>>
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-23 05:44 , Processed in 0.449484 second(s), Total 75, Slave 58 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号