3

我正在尝试做基本的接收器,并让它在电池状态发生变化时显示祝酒词,但是当一个人发生变化时,所有其他祝酒词也会被执行。

我正在通过 telnet 使用 AVD 操作电池

public class battery1 extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        String strCharge = null, strHealth = null, strPlug = null;
        int status, health, plugged, temp;

        status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        String action = intent.getAction();

        if (intent.hasExtra(BatteryManager.EXTRA_STATUS)) {
            switch(status) {
            case BatteryManager.BATTERY_STATUS_CHARGING:
                strCharge = "Charging";
                break;
            case BatteryManager.BATTERY_STATUS_DISCHARGING:
                strCharge = "Discharging";
                break;
            case BatteryManager.BATTERY_STATUS_FULL:
                strCharge = "Full";
                break;
            case BatteryManager.BATTERY_STATUS_UNKNOWN:
                strCharge = "Unknown";
                break;
            }

            Toast.makeText(context,"Charging status: " + strCharge, Toast.LENGTH_SHORT).show();
        }

        health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, -1);
        if (intent.hasExtra(BatteryManager.EXTRA_HEALTH))
        {
            switch (health) {
            case BatteryManager.BATTERY_HEALTH_DEAD:
                strHealth = "Dead";
                break;
            case BatteryManager.BATTERY_HEALTH_GOOD:
                strHealth = "Good";
                break;
            case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
                strHealth = "Over voltage";
                break;
            case BatteryManager.BATTERY_HEALTH_OVERHEAT:
                strHealth = "Overheat";
                break;
            case BatteryManager.BATTERY_HEALTH_UNKNOWN:
                strHealth = "Unknown";
                break;
            }

            Toast.makeText(context,"Battery health is " + strHealth, Toast.LENGTH_SHORT).show();
        }

    }
}
4

0 回答 0