试试这段代码,可能会对你有所帮助:
private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context arg0,Intent intent) {
// TODO Auto-generated method stub
//this will give you battery current status
int level = intent.getIntExtra("level",0);
contentTxt.setText(String.valueOf(level) + "%");
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS,-1);
textView2.setText("status:"+status);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
textView3.setText("is Charging:"+isCharging);
int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED,-1);
textView4.setText("is Charge plug:"+chargePlug);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
textView5.setText("USB Charging:"+usbCharge+" AC charging:"+acCharge);
}
};
在主类中注册这个使用:
this.registerReceiver(this.mBatInfoReceiver,new IntentFilter(Intent.ACTION_BATTERY_CHANGED));