Android电池状态监听实例1(下)
2011-12-09 10:25
323 查看
文件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电池状态的监听》
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电池状态的监听》
相关文章推荐
- Android电池状态监听实例1
- Android监听电池状态实例代码
- Android电池状态监听实例1(上)
- Android电池状态监听
- Android 网络状态实时监听代码实例(一)
- Android开发:优化电池续航-监听电量和充电状态
- Android监听电池状态
- Android监听电池状态
- Android 监听软键盘状态的实例详解
- Android 广播监听网络状态详解及实例代码
- Android网络状态实时监听实例代码(二)
- Android电池状态的监听
- android学习之Service的笔记,里面service里有监听用户通话状态的实例
- android广播接收者实例_app状态监听
- Android设备的电池续航时间优化(Optimizing Battery Life)——(一)监听电池电量和电池的充电状态
- Android设备的电池续航时间优化(Optimizing Battery Life)——(一)监听电池电量和电池的充电状态
- Android电池状态的监听
- 【android】 监听去电和来电电话状态
- android电话状态监听
- Android 电话状态的监听和回调