转载自:http://blog.csdn.net/chrp99/article/details/9137349
public class MainActivity extends Activity {
class BtteryReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("+++++++++++++++++++++++");
// 判断它是否是为电量变化的Broadcast Action
if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
// 获取当前电量
int level = intent.getIntExtra("level", 0);
// 电量的总刻度
int scale = intent.getIntExtra("scale", 0);
// 把它转换成百分比
String content = ("电池电量为" + ((level * 100) / scale) + "%");
int key = ((level * 100) / scale);
switch (key) {
case 100:
Toast.makeText(context, "电池已经充满", Toast.LENGTH_LONG).show();
break;
case 50:
Toast.makeText(context, "电池已经使用一半", Toast.LENGTH_LONG)
.show();
break;
case 30:
Toast.makeText(context, "电池已到30%了", Toast.LENGTH_LONG)
.show();
break;
case 40:
Toast.makeText(context, "电池已到31%了", Toast.LENGTH_LONG)
.show();
break;
case 10:
Toast.makeText(context, "电池不足10%", Toast.LENGTH_LONG)
.show();
break;
case 9:
Toast.makeText(context, "电池不足9%", Toast.LENGTH_LONG).show();
break;
case 5:
Toast.makeText(context, "电池不足5%", Toast.LENGTH_LONG).show();
break;
}
}
}
}
另一篇博客
Android开发者可以利用BroadcastReceiver机制,获取电池电量变化的ACTION_BATTERY_CHANGED intent,进而获取当前android设备的电池状态。
具体操作步骤:
1. 创建一个监听ACTION_BATTERY_CHANGED事件的intentFilter。
2. 创建一个BroadcastReceiver对象,该对象可以接收broadcast intent。
3. 注册BroadcastReceiver对象来监听ACTION_BATTERY_CHANGED事件。
4. 在BroadcastReceiver对象中,重写onReceive方法,在onReceive方法的传入参数intent里获取需要的电池状态信息。
5. 在生命周期结束时,取消step4中的注册。
[java]view
plaincopyprint?
public class BatteryMonitorActivity extends Activity {
private TextView batterLevel;
private BroadcastReceiver batteryLevelRcvr;
private IntentFilter batteryLevelFilter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
batterLevel = (TextView) findViewById(R.id.batteryLevel);
monitorBatteryState();
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(batteryLevelRcvr);
}
private void monitorBatteryState() {
batteryLevelRcvr = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
StringBuilder sb = new StringBuilder();
int rawlevel = intent.getIntExtra("level", -1);
int scale = intent.getIntExtra("scale", -1);
int status = intent.getIntExtra("status", -1);
int health = intent.getIntExtra("health", -1);
int level = -1; // percentage, or -1 for unknown
if (rawlevel >= 0 && scale > 0) {
level = (rawlevel * 100) / scale;
}
sb.append("The phone");
if (BatteryManager.BATTERY_HEALTH_OVERHEAT == health) {
sb.append("'s battery feels very hot!");
} else {
switch (status) {
case BatteryManager.BATTERY_STATUS_UNKNOWN:
sb.append("no battery.");
break;
case BatteryManager.BATTERY_STATUS_CHARGING:
sb.append("'s battery");
if (level <= 33)
sb.append(" is charging, battery level is low"
+ "[" + level + "]");
else if (level <= 84)
sb.append(" is charging." + "[" + level + "]");
else
sb.append(" will be fully charged.");
break;
case BatteryManager.BATTERY_STATUS_DISCHARGING:
case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
if (level == 0)
sb.append(" needs charging right away.");
else if (level > 0 && level <= 33)
sb.append(" is about ready to be recharged, battery level is low"
+ "[" + level + "]");
else
sb.append("'s battery level is" + "[" + level + "]");
break;
case BatteryManager.BATTERY_STATUS_FULL:
sb.append(" is fully charged.");
break;
default:
sb.append("'s battery is indescribable!");
break;
}
}
sb.append(' ');
batterLevel.setText(sb.toString());
}
};
batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(batteryLevelRcvr, batteryLevelFilter);
}
}
[html]view
plaincopyprint?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/batteryLevel"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal"
android:textSize="50dip">
</TextView>
</LinearLayout>
Android的上层实现显示手机电池电量是通过监听实现,当电池电量改变时会通知到程序。
当然这个有朋友就要问了我不是想监听电池电量,我是想直接获取当前的电池电量。
其实这是同一个问题,当你在初始化监听器的时候他会自动首先获取电池的电量信息,然后后续当电池改变时同时也会通知当前电池电量的改变情况。
转自
http://www.blogjava.net/zhangqian1216/archive/2012/01/04/367858.html
http://blog.macrowen.com/Technology/3074.html
记得加权限