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;

        <a href="http://home.51cto.com/index.php?s=/space/5017954" target="_blank">@Override</a>
        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</a>
                        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</a>
        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</a>
        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>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值