原理概述:

    手机电池电量的获取在应用程序的开发中也很常用,Android系统中手机电池电量发生变化的消息是通过Intent广播来实现的,常用的Intent的Action有  Intent.ACTION_BATTERY_CHANGED(电池电量发生改变时)、Intent.ACTION_BATTERY_LOW(电池电量达到下限时)、和Intent.ACTION_BATTERY_OKAY(电池电量从低恢复到高时)。

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

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:orientation="vertical" >
 6 
 7     <Button
 8         android:id="@+id/button"
 9         android:layout_width="fill_parent"
10         android:layout_height="wrap_content"
11         android:text="取得电池电量" />
12 
13 </LinearLayout>


 1 package org.gl.demo;
 2 
 3 import android.app.Activity;
 4 import android.content.Intent;
 5 import android.content.IntentFilter;
 6 import android.os.Bundle;
 7 import android.view.View;
 8 import android.view.View.OnClickListener;
 9 import android.widget.Button;
10 
11 public class MainActivity extends Activity {
12     private Button button = null;
13 
14     @Override
15     public void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.main);
18 
19         button = (Button) findViewById(R.id.button);
20         button.setOnClickListener(new BatteryClickListener());
21     }
22 
23     private class BatteryClickListener implements OnClickListener {
24 
25         @Override
26         public void onClick(View v) {
27             BatteryBroadcastReceiver receiver = new BatteryBroadcastReceiver();
28             IntentFilter filter = new IntentFilter(
29                     Intent.ACTION_BATTERY_CHANGED);
30             MainActivity.this.registerReceiver(receiver, filter);
31 
32         }
33 
34     }
35 }

 1 package org.gl.demo;
 2 
 3 import android.app.AlertDialog;
 4 import android.app.Dialog;
 5 import android.content.BroadcastReceiver;
 6 import android.content.Context;
 7 import android.content.DialogInterface;
 8 import android.content.Intent;
 9 
10 public class BatteryBroadcastReceiver extends BroadcastReceiver {
11 
12     @Override
13     public void onReceive(Context context, Intent intent) {
14         if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
15             // 获得当前电量
16             int current = intent.getIntExtra("level"0);
17             // 获取总电量
18             int total = intent.getIntExtra("scale"0);
19             Dialog dialog = new AlertDialog.Builder(context)
20                     .setTitle("电池电量")
21                     .setMessage(
22                             "电池电量为:" + String.valueOf(current * 100 / total)
23                                     + "%")
24                     .setNegativeButton("关闭",
25                             new DialogInterface.OnClickListener() {
26 
27                                 @Override
28                                 public void onClick(DialogInterface dialog,
29                                         int which) {
30 
31                                 }
32                             }).create();
33             dialog.show();
34         }
35 
36     }
37 
38 }
39