查询电池容量 [英] Query for Battery capacity

查看:526
本文介绍了查询电池容量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助。我只想让应用程序读取电池的容量,就像毫安/毫安读。任何人都可以帮助我吗?

我读过有关这另一个线程,但我很困惑,因为我需要从电池容量整数。例如,我的Andr​​oid有容量2500毫安时的电池
我需要,我想在我的计算,这个数字能力(2500)的该整数。

感谢您的帮助。

这是code,我想改变,我只是困惑的地方必须改变。

 公共无效getBatteryCapacity(){
        对象mPowerProfile_ = NULL;        最后弦乐POWER_PROFILE_CLASS =com.android.internal.os.PowerProfile;        尝试{
            mPowerProfile_ =的Class.forName(POWER_PROFILE_CLASS)
                    .getConstructor(Context.class).newInstance(本);
        }赶上(例外五){
            e.printStackTrace();
        }        尝试{
            双batteryCapacity =(双人间)班
                    .forName(POWER_PROFILE_CLASS)
                    .getMethod(getAveragePower,java.lang.String.class)
                    .invoke(mPowerProfile_battery.capacity);
            Toast.makeText(MainActivity.this,batteryCapacity +麻将
                    Toast.LENGTH_LONG).show();
        }赶上(例外五){
            e.printStackTrace();
        }
    }


解决方案

是的,你的code给出总容量毫安。您可以更改函数返回这样的值:

 公共双getBatteryCapacity(){  //电源配置类实例
  对象mPowerProfile_ = NULL;  //重置变量的电池容量
  双batteryCapacity = 0;  //电源配置类名
  最后弦乐POWER_PROFILE_CLASS =com.android.internal.os.PowerProfile;  尝试{    //获取电源配置类并创建实例。我们必须做到这一点
    //由于动态包android.internal不是公共API的一部分
    mPowerProfile_ =的Class.forName(POWER_PROFILE_CLASS)
                    .getConstructor(Context.class).newInstance(本);  }赶上(例外五){    //类没有发现?
    e.printStackTrace();
  }  尝试{    //调用PowerProfile法getAveragePower与参数battery.capacity
    batteryCapacity =(双人间)班
                    .forName(POWER_PROFILE_CLASS)
                    .getMethod(getAveragePower,java.lang.String.class)
                    .invoke(mPowerProfile_battery.capacity);  }赶上(例外五){    // 出了些问题
    e.printStackTrace();
  }    返回batteryCapacity;
 }

getAveragePower 函数返回的平均电流
在毫安由子系统消耗。在这种情况下子系统字符串 battery.capacity 返回电池的容量。

请参阅类code在这里:
<一href=\"https://android.googlesource.com/platform/frameworks/base.git/+/master/core/java/com/android/internal/os/PowerProfile.java\" rel=\"nofollow\">https://android.googlesource.com/platform/frameworks/base.git/+/master/core/java/com/android/internal/os/PowerProfile.java

如果你真的想要的价值为int,只是改变这样的:

  INT BC = getBatteryCapacity()的intValue();

I need help. I just want to make application that reads battery capacity, like read in mAh/mA. Anyone can help me please?

I've read another thread about this, but I was confused because I need an integer from battery capacity. For example, my android has a battery with capacity 2500 mAh and I need that integer of capacity(2500) where I want to include that number in my calculation.

Thanks for the help.

This is code that I want to change, I am just confused where it must be changed.

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("getAveragePower", java.lang.String.class)
                    .invoke(mPowerProfile_, "battery.capacity");
            Toast.makeText(MainActivity.this, batteryCapacity + " mah",
                    Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            e.printStackTrace();
        } 
    }

解决方案

Yes, your code gives total mAh capacity. You can change that function to return the value like this:

public Double getBatteryCapacity() {

  // Power profile class instance
  Object mPowerProfile_ = null;

  // Reset variable for battery capacity
  double batteryCapacity = 0;

  // Power profile class name 
  final String POWER_PROFILE_CLASS = "com.android.internal.os.PowerProfile";

  try {

    // Get power profile class and create instance. We have to do this 
    // dynamically because android.internal package is not part of public API
    mPowerProfile_ = Class.forName(POWER_PROFILE_CLASS)
                    .getConstructor(Context.class).newInstance(this);

  } catch (Exception e) {

    // Class not found?
    e.printStackTrace();
  } 

  try {

    // Invoke PowerProfile method "getAveragePower" with param "battery.capacity"
    batteryCapacity = (Double) Class
                    .forName(POWER_PROFILE_CLASS)
                    .getMethod("getAveragePower", java.lang.String.class)
                    .invoke(mPowerProfile_, "battery.capacity");

  } catch (Exception e) {

    // Something went wrong
    e.printStackTrace();
  } 

    return batteryCapacity;
 }

The getAveragePower function returns the average current in mA consumed by the subsystem. In this case subsystem string is battery.capacity which returns the battery capacity.

See class code here: https://android.googlesource.com/platform/frameworks/base.git/+/master/core/java/com/android/internal/os/PowerProfile.java

And if you really want that value as int, just change it like this:

int bc = getBatteryCapacity().intValue();

这篇关于查询电池容量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆