private BatteryReceiver receiver=null;

receiver=new BatteryReceiver();
IntentFilter filter=new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(receiver, filter);//注册BroadcastReceiver

 

private class BatteryReceiver extends BroadcastReceiver{
  @Override
  public void onReceive(Context context, Intent intent) {
   int current=intent.getExtras().getInt("level");//获得当前电量
   int total=intent.getExtras().getInt("scale");//获得总电量
   int percent=current*100/total;
   textView5.setText("现在的电量是"+percent+"%。");
  }
 }