这段代码在每一个设备上都给了我2000的答案,而之前关于这个问题的所有问题都给出了不相关的答案。
有人帮忙吗?
public void getBatteryCapacity() {
Object mPowerProfile_ = null;
final String POWER_PROFILE_CLASS = "com.android.internal.os.PowerProfile";
try {
mPowerProfile_ = Class.forName(POWER_PROFILE_CLASS)
.getConstructor(Context.class).newInstance(getContext());
} catch (Exception e) {
e.printStackTrace();
}
try {
double batteryCapacity = (Double) Class
.forName(POWER_PROFILE_CLASS)
.getMethod("getAveragePower", java.lang.String.class)
.invoke(mPowerProfile_, "battery.capacity");
Toast.makeText(getActivity(), batteryCapacity + " mah",
Toast.LENGTH_LONG).show();
Log.d("Capacity",batteryCapacity+" mAh");
} catch (Exception e) {
e.printStackTrace();
}
}但我想要最大容量,就像CPU-Z应用程序提供的那样:

发布于 2016-09-06 10:32:45
从附带的文档以及PowerProfile类的源代码中,getAveragePower()方法返回:
milliAmps中的平均电流。
相反,您必须使用getBatteryCapacity(),它返回
mAh中的电池容量
因此,您必须更改方法调用,因为getBatteryCapacity()不使用任何参数,不像getAveragePower()使用两个参数,因此代码如下:
public void getBatteryCapacity() {
Object mPowerProfile_ = null;
final String POWER_PROFILE_CLASS = "com.android.internal.os.PowerProfile";
try {
mPowerProfile_ = Class.forName(POWER_PROFILE_CLASS)
.getConstructor(Context.class).newInstance(this);
} catch (Exception e) {
e.printStackTrace();
}
try {
double batteryCapacity = (Double) Class
.forName(POWER_PROFILE_CLASS)
.getMethod("getBatteryCapacity")
.invoke(mPowerProfile_);
Toast.makeText(MainActivity.this, batteryCapacity + " mah",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}https://stackoverflow.com/questions/39346057
复制相似问题