解码电池值

时间:2013-10-28 06:23:10

标签: android

在电池广播接收器中,我有以下3行代码:

int health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, 0);
int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
int voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0);

以上三个的值为2,2和4。

我如何解码这些值并向用户展示对健康有意义的事情,如果电池健康状况良好,不良或需要更换,我不能知道计算机是怎样的。

提前致谢

2 个答案:

答案 0 :(得分:2)

将此用作电池

switch (health) {
    case BatteryManager.BATTERY_HEALTH_DEAD:

        break;
    case BatteryManager.BATTERY_HEALTH_GOOD:

        break;
    case BatteryManager.BATTERY_HEALTH_COLD:

        break;
    case BatteryManager.BATTERY_HEALTH_OVERHEAT:

        break;
    case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:

        break;

    default:
        break;
    }

用于插入

switch (plugged ) {
    case BatteryManager.BATTERY_PLUGGED_AC:

        break;
    case BatteryManager.BATTERY_PLUGGED_USB:

        break;
    case BatteryManager.BATTERY_PLUGGED_WIRELESS:

        default:
        break;
    }

用于电压

switch (voltage) {
    case BatteryManager.BATTERY_STATUS_CHARGING:

        break;
    case BatteryManager.BATTERY_STATUS_DISCHARGING:

        break;
    case BatteryManager.BATTERY_STATUS_FULL:
        break;
    case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
        break;
    case BatteryManager.BATTERY_STATUS_UNKNOWN:
        break;
    default:
        break;
    }

答案 1 :(得分:2)

在onCreate方法下使用

@Override
public void onCreate() {
    BroadcastReceiver batteryReceiver = new BroadcastReceiver() {
        int scale = -1;
        int level = -1;
        int voltage = -1;
        int temp = -1;
        @Override
        public void onReceive(Context context, Intent intent) {
            level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
            scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
            temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
            voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1);
            Log.e("BatteryManager", "level is "+level+"/"+scale+", temp is "+temp+", voltage is "+voltage);
        }
    };
    IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    registerReceiver(batteryReceiver, filter);
}

如果等级= 20/100表示​​电池剩余20% 温度是320然后温度是32度celcuis。 如果电压为4000,那么电压为4.000伏