android 中如何获取电池的电流值

Life is short , play more!
本文来自lihao's Blog,转载请注明。

你只需要看看current widget的源代码就好. 方法是,通过sysfs 文件系统里的对应文件, 获取电流值. 但是不同型号的手机的系统,电流值的获取文件不太相同, 具体请参见代码.

package com.manor.currentwidget.library;

import java.io.File;

import android.os.Build;
import android.util.Log;

public class CurrentReaderFactory {

static public Long getValue() {

File f = null;

// htc desire hd / desire z / inspire?
if (Build.MODEL.toLowerCase().contains("desire hd") ||
Build.MODEL.toLowerCase().contains("desire z") ||
Build.MODEL.toLowerCase().contains("inspire")) {

f = new File("/sys/class/power_supply/battery/batt_current");
if (f.exists()) {
return OneLineReader.getValue(f, false);
}
}

// nexus one cyangoenmod
f = new File("/sys/devices/platform/ds2784-battery/getcurrent");
if (f.exists()) {
return OneLineReader.getValue(f, true);
}

// sony ericsson xperia x1
f = new File("/sys/devices/platform/i2c-adapter/i2c-0/0-0036/power_supply/ds2746-battery/current_now");
if (f.exists()) {
return OneLineReader.getValue(f, false);
}

// xdandroid
/*if (Build.MODEL.equalsIgnoreCase("MSM")) {*/
f = new File("/sys/devices/platform/i2c-adapter/i2c-0/0-0036/power_supply/battery/current_now");
if (f.exists()) {
return OneLineReader.getValue(f, false);
}
/*}*/

// droid eris
f = new File("/sys/class/power_supply/battery/smem_text");
if (f.exists()) {
Long value = SMemTextReader.getValue();
if (value != null)
return value;
}

// htc sensation / evo 3d
f = new File("/sys/class/power_supply/battery/batt_attr_text");
if (f.exists())
{
Long value = BattAttrTextReader.getValue();
if (value != null)
return value;
}

// some htc devices
f = new File("/sys/class/power_supply/battery/batt_current");
if (f.exists())
return OneLineReader.getValue(f, false);

// nexus one
f = new File("/sys/class/power_supply/battery/current_now");
if (f.exists())
return OneLineReader.getValue(f, true);

// samsung galaxy vibrant
f = new File("/sys/class/power_supply/battery/batt_chg_current");
if (f.exists())
return OneLineReader.getValue(f, false);

// sony ericsson x10
f = new File("/sys/class/power_supply/battery/charger_current");
if (f.exists())
return OneLineReader.getValue(f, false);

// Nook Color
f = new File("/sys/class/power_supply/max17042-0/current_now");
if (f.exists())
return OneLineReader.getValue(f, false);
return null;
}
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注