Android电量监控

简单介绍

  Android系统中手机电池电量发生变化的消息是通过Intent广播来实现的

  • Intent.ACTION_BATTERY_CHANGED(电池电量发生改变时)
  • Intent.ACTION_BATTERY_LOW(电池电量达到下限时)、这个值可以修改的,0-100level可选
  • Intent.ACTION_BATTERY_OKAY(电池电量从低恢复到高时)

当需要在程序中获取电池电量的信息时,需要为应用程序注册BroadcastReceiver(动态注册),当特定的Action事件发生时,系统将会发出相应的广播,应用程序就可以通过BroadcastReceiver来接受广播,并进行相应的处理。

代码

/**
 * 电量监控
 * 
 * 
 */
public class BatteryActivity extends Activity implements OnClickListener {

        private Button startBtn, stopBtn;
        private TextView batteryValue;
        private BroadcastReceiver mReceiver;
        private IntentFilter mFilter;

        "http://home.51cto.com/index.php?s=/space/5017954" target="_blank">@Override
        protected void onCreate(Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                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
                                // BatteryManager 包含了Intent.ACTION_BATTERY_CHANGED所需的String和常量值
                                // 当前电量
                                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);
        }
}

布局文件

"1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    

你可能感兴趣的:(android,电池)