通过一下代码方式就能时时的获取电量的变化信息,也可以通过BatteryManager对象里面的其他String 来获取其他信息,比如EXTRA_PLUGGED,这个是返回当前是用usb,交流电,无线电的方式等, 这里是比较简单的几行代码通过广播监听设备变化:


public class BatteryActivity extends Activity implements OnClickListener {


        private Button startBtn, stopBtn;

        private TextView batteryValue;

        private BroadcastReceiver mReceiver;

        private IntentFilter mFilter;


        @Override

        protected void onCreate(Bundle savedInstanceState) {

                super.onCreate(savedInstanceState);

                setContentView(R.layout.battery_layout);

                startBtn = (Button) findViewById(R.id.start_btn);
                stopBtn = (Button) findViewById(R.id.stop_btn);
                batteryValue = (TextView) findViewById(R.id.battery_vaule);
                batteryValue.setTextColor(Color.RED);

                startBtn.setOnClickListener(this);
                stopBtn.setOnClickListener(this);

                mFilter = new IntentFilter();
                // 监听电量变化,只能采用动态注册方式,不能在AndroidManifest.xml中用静态注册广播接受者

                mFilter.addAction(Intent.ACTION_BATTERY_CHANGED);


                mReceiver = new BroadcastReceiver() {

                        @Override

                        public void onReceive(Context context, Intent intent) {
                                // TODO Auto-generated method stub<br data-

                                // BatteryManager 包含了Intent.ACTION_BATTERY_CHANGED所需的String和常量值<br data-filtered="filtered">

                                // 当前电量
                                int level =intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);

                                // 最大电量
                                int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);
                                batteryValue.setText("当前电量为:" + (level * 100) / scale + "%");

                        }
                };

        }
        @Override
        public void onClick(View v) {
                // TODO Auto-generated method stub
                int id = v.getId();
                switch (id) {
                case R.id.start_btn:
                        registerReceiver(mReceiver, mFilter);

                        break;

                case R.id.stop_btn:<
                        unregisterReceiver(mReceiver);
                        break;

                default:
                        break;
                }
        }
      
        @Override
        protected void onDestroy() {

                // TODO Auto-generated method stub<
                super.onDestroy();
                unregisterReceiver(mReceiver);
        }
}

布局文件: 

	<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <Button
        android:id="@+id/start_btn"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:text="start" />
    <Button
        android:id="@+id/stop_btn"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:text="stop" />
    <TextView
        android:id="@+id/battery_vaule"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp" />
</LinearLayout>