1

在电池广播接收器中,我有以下 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。

我如何解码这些值并显示对用户有意义的东西,比如健康,如果电池健康状况好、坏或需要更换,我不能告诉我计算机的情况。

提前致谢

4

2 回答 2

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;
    }
于 2013-10-28T06:26:37.523 回答
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);
}

如果 level = 20/100 表示电池电量为 20%,剩余温度为 320,则温度为 32 摄氏度。如果电压为 4000,则电压为 4.000 伏

于 2013-10-28T06:29:31.513 回答