一个测试电池状态的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充电时截图如下:
- 一个测试电池状态的actiivity
- actiivity的上下左右滑动动画
- Android电池状态的监听
- 监控iPhone的电池状态
- Android电池状态的监听
- Android 对电池状态的监视
- Android 对电池状态的监视
- OSX: 显示电池状态的命令行
- UIDevice类实现电池状态的检测
- Android判断手机的电池状态
- JavaScript里获取电池状态的方法
- JavaScript里获取电池状态的方法
- Android获取电池充电状态的方式
- Android获取电池充电状态的方式
- 一个简单的测试:iPhone电池续航时间短并非硬件问题
- 电池_电池容量的测试——手机、电池、充电器三件套之电池篇3
- Android测试电池温度的小程序
- 一个简单的android电池显示layout
- mysql触发器的作用及语法
- 跟ini文件相关的api
- Lua初学
- openam + opendj 安装
- 只允许遍历一遍字符串,找出字符串中第一个只出现一次的字符
- 一个测试电池状态的actiivity
- 【Android基础入门〖14〗】SharedPreferences
- Poj1149 PIGS (经典网络流)
- Sprite Kit 高级场景处理
- 探索推荐引擎内部的秘密,第 3 部分: 深入推荐引擎相关算法 - 聚类
- HDU2795 Billboard
- “黑马程序员“宣传语有奖大征集
- JAVA集合
- Windows Storage Server 2008 R2 Standard(64位)之ASM(Automated Storage Manager)管理