一个测试电池状态的actiivity

来源:互联网 发布:网络编程 博客园 编辑:程序博客网 时间:2024/05/01 19:06

        如何在一个页面中完整的显示当前电池的状态呢,诸如电压、百分比、电池充电状态等。以下的一个简单程序可达到这个目的。

import android.os.BatteryManager;import android.os.Bundle;import android.app.Activity;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.widget.TextView;

        在页面中将会显示8个状态,所以需要在页面定义如下textview

private TextView mStatus;//电池状态private TextView mPower;//充电源private TextView mLevel;//百分比private TextView mScale;//最大量度private TextView mHealth;//电池健康private TextView mVoltage;//电池电压private TextView mTechnology;//电池种类private TextView mBatteryid;//电池ID
private IntentFilter   mIntentFilter;

          onCreate完成绑定显示控件和注册receiver,关键是响应系统ACTION_BATTERY_CHANGED这个INTENT。

        mIntentFilter = new IntentFilter();        mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);//添加ACTION_BATTERY_CHANGED的filter        mStatus = (TextView)findViewById(R.id.status);        mPower = (TextView)findViewById(R.id.power);        mLevel = (TextView)findViewById(R.id.level);        mScale = (TextView)findViewById(R.id.scale);        mHealth = (TextView)findViewById(R.id.health);        mTechnology = (TextView)findViewById(R.id.technology);        mVoltage = (TextView)findViewById(R.id.voltage);        mBatteryid = (TextView)findViewById(R.id.batteryid);        registerReceiver(mIntentReceiver, mIntentFilter);

        在mIntentReceiver完成对电池信息的读取

private BroadcastReceiver mIntentReceiver = new BroadcastReceiver(){public void onReceive(Context context, Intent intent) {String action = intent.getAction();if (action.equals(Intent.ACTION_BATTERY_CHANGED)){mLevel.setText(getString(R.string.battery_info_level_label) + ":"+intent.getIntExtra("level", 0));mScale.setText(getString(R.string.battery_info_scale_label) + ":"+intent.getIntExtra("scale", 0));mVoltage.setText(getString(R.string.battery_info_voltage_label) + ":"+intent.getIntExtra("voltage", 0));mBatteryid.setText(getString(R.string.battery_info_id_label) + ":"+intent.getIntExtra("batteryid", 0));mTechnology.setText(getString(R.string.battery_info_technology_label) + ":"+intent.getStringExtra("technology"));//以上五个信息从Intent的参数可以直接获取到int plugType = intent.getIntExtra("plugged", 0);int status = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);String statusString;statusString = getString(R.string.battery_info_status_label)+":";if (status == BatteryManager.BATTERY_STATUS_CHARGING) {                    statusString += getString(R.string.battery_info_status_charging);                    if (plugType > 0) {                    mStatus.setTextColor(0xff0033ff);                        statusString = statusString + " " + getString(                                (plugType == BatteryManager.BATTERY_PLUGGED_AC)//AC充电还是USB充电                                        ? R.string.battery_info_status_charging_ac                                        : R.string.battery_info_status_charging_usb);                    }                } else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING) {                mStatus.setTextColor(0xffff33ff);                    statusString += getString(R.string.battery_info_status_discharging);                } else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) {                mStatus.setTextColor(0xffff33ff);                    statusString += getString(R.string.battery_info_status_not_charging);                } else if (status == BatteryManager.BATTERY_STATUS_FULL) {                mStatus.setTextColor(0xff33ff00);                    statusString += getString(R.string.battery_info_status_full);                } else {                mStatus.setTextColor(0xffff3366);                    statusString += getString(R.string.battery_info_status_unknown);                }                mStatus.setText(statusString);                                switch (plugType) {                case 0:                    mPower.setText(getString(R.string.battery_info_power_label) + ":"+getString(R.string.battery_info_power_unplugged));                    break;                case BatteryManager.BATTERY_PLUGGED_AC:                    mPower.setText(getString(R.string.battery_info_power_label) + ":"+getString(R.string.battery_info_power_ac));                    break;                case BatteryManager.BATTERY_PLUGGED_USB:                    mPower.setText(getString(R.string.battery_info_power_label) + ":"+getString(R.string.battery_info_power_usb));                    break;                case (BatteryManager.BATTERY_PLUGGED_AC|BatteryManager.BATTERY_PLUGGED_USB):                    mPower.setText(getString(R.string.battery_info_power_label) + ":"+getString(R.string.battery_info_power_ac_usb));                    break;                default:                    mPower.setText(getString(R.string.battery_info_power_label) + ":"+getString(R.string.battery_info_power_unknown));                    break;                }                                int health = intent.getIntExtra("health", BatteryManager.BATTERY_HEALTH_UNKNOWN);                String healthString;                healthString = getString(R.string.battery_info_health_label)+":";                if (health == BatteryManager.BATTERY_HEALTH_GOOD) {                    healthString += getString(R.string.battery_info_health_good);                } else if (health == BatteryManager.BATTERY_HEALTH_OVERHEAT) {                    healthString += getString(R.string.battery_info_health_overheat);                } else if (health == BatteryManager.BATTERY_HEALTH_DEAD) {                    healthString += getString(R.string.battery_info_health_dead);                } else if (health == BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE) {                    healthString += getString(R.string.battery_info_health_over_voltage);                } else if (health == BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE) {                    healthString += getString(R.string.battery_info_health_unspecified_failure);                } else {                    healthString += getString(R.string.battery_info_health_unknown);                }                mHealth.setText(healthString);}}};

        完成后在USB充电时截图如下:


原创粉丝点击