注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

民主与科学

独立之人格,自由之思想

 
 
 

日志

 
 

Android电池状态监听实例1(下)  

2011-05-29 11:04:54|  分类: Android其他 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
文件4:BatteryInfo.java
package com.teleca.robin;
import android.content.Intent;
import android.os.BatteryManager;
public class BatteryInfo {
int health;
int batteryIconId;
int plugged;
boolean batteryPresent;
int status;
String technology;
int temperature;
int voltage;
int level;
int scale;
public BatteryInfo(Intent intent)
{
String action = intent.getAction();
if (!Intent.ACTION_BATTERY_CHANGED.equals(action))
return;
health=intent.getIntExtra(BatteryManager.EXTRA_HEALTH,BatteryManager.BATTERY_HEALTH_UNKNOWN);
batteryIconId=intent.getIntExtra(BatteryManager.EXTRA_ICON_SMALL,-1);
plugged=intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
batteryPresent=intent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, false);
status=intent.getIntExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_UNKNOWN);
technology=intent.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY);
temperature=intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
voltage=intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1);
level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
}
public String toString()
{
StringBuffer strBuffer=new StringBuffer(50);
strBuffer.append("information for battery\n");
strBuffer.append("health :");
switch(health)
{
case BatteryManager.BATTERY_HEALTH_DEAD:
strBuffer.append("BATTERY_HEALTH_DEAD");
break;
case BatteryManager.BATTERY_HEALTH_GOOD:
strBuffer.append("BATTERY_HEALTH_GOOD");
break;
case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
strBuffer.append("BATTERY_HEALTH_OVER_VOLTAGE");
break;
case BatteryManager.BATTERY_HEALTH_UNKNOWN:
strBuffer.append("BATTERY_HEALTH_UNKNOWN");
break;
case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
strBuffer.append("BATTERY_HEALTH_UNSPECIFIED_FAILURE");
break;
default:
strBuffer.append("BATTERY_HEALTH_UNKNOWN");
break;
}
strBuffer.append("\n");
strBuffer.append("iconId:"+batteryIconId);
strBuffer.append("\n");
strBuffer.append("Power source:");
switch(plugged)
{
case 0:
strBuffer.append("batter");
break;
case BatteryManager.BATTERY_PLUGGED_AC:
strBuffer.append("AC charger");
break;
case BatteryManager.BATTERY_PLUGGED_USB:
strBuffer.append("USB charger");
break;
default:
strBuffer.append("unkonow charger");
break;
}
strBuffer.append("\n");
strBuffer.append("present:"+batteryPresent);
strBuffer.append("\n");
strBuffer.append("status: ");
switch(status)
{
case BatteryManager.BATTERY_STATUS_CHARGING:
strBuffer.append("charging");
break;
case BatteryManager.BATTERY_STATUS_DISCHARGING:
strBuffer.append("discharging");
break;
case BatteryManager.BATTERY_STATUS_FULL:
strBuffer.append("full");
break;
case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
strBuffer.append("not charing");
break;
}
strBuffer.append("\n");
strBuffer.append("technology:"+technology);
strBuffer.append("\n");
strBuffer.append("温度: "+temperature+"\n");
strBuffer.append("电压:"+voltage+"\n");
strBuffer.append("剩余电量:" + String.valueOf(level * 100 / scale)
+ "%");
return strBuffer.toString();
}

}
注意:更多内容请参考《Android电池状态的监听
  评论这张
 
阅读(600)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017