橘子味的心
标题:6.2 Android BroadcastReceiver

广播(Broadcast)是 Android 系统中应用程序间通信的手段。

当有特定事件发生时,例如有来电、有短信、电池电量变化等事件发生时,Android 系统都会产生特定的 Intent 对象并且自动进行广播,而针对特定事件注册的 BroadcastReceiver 会接收到这些广播,并获取 Intent 对象中的数据进行处理。

在广播 Intent 对象时可以指定用户权限,以此限制仅有获得了相应权限的 BroadcastReceiver 才能接收并处理对应的广播。

BroadcastReceiver 有动态和静态两种注册方法。
  • 动态注册方法即使用 Context. registerReceiver() 方法进行注册,需要特别注意的是,动态注册方法在退出程序前要使用 Context.unregisterReceiver() 方法撤销注册。
  • 静态注册方法即在 AndroidManifest.xml 文件中通过 <receiver> 标签进行注册。

一个 BroadcastReceiver 对象只有在被调用 onReceive(Context, Intent) 时才有效,当从该函数返回后,该对象就已无效了,其生命周期结束。

下面介绍如何使用动态注册来实现监听电池剩余电量。

实例 BatteryDemo 演示了使用动态注册 BroadcastReceiver 对象并且接收系统电量改变事件并加以处理的过程,运行效果如图 1 所示。
BatteryDemo的运行效果
图 1  BatteryDemo的运行效果

实例 BatteryDemo 中 main.xml 的代码如下:
  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. <ToggleButton
  8. android:id="@+id/button"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:textOff="停止检测"
  12. android:textOn="检测当前手机电量" />
  13.  
  14. <TextView
  15. android:id="@+id/text"
  16. android:layout_width="fill_parent"
  17. android:layout_height="wrap_content" />
  18. </LinearLayout>
实例 BatteryDemo 中 AndroidManifest.xml 的代码如下:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="introduction.android.batterydemo"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6.  
  7. <uses-sdk android:minSdkVersion="14" />
  8. <application
  9. android:allowBackup="true"
  10. android:icon="@mipmap/ic_launcher"
  11. android:label="@string/app_name"
  12. android:roundIcon="@mipmap/ic_launcher_round"
  13. android:supportsRtl="true"
  14. android:theme="@style/AppTheme">
  15. <activity android:name=".MainActivity">
  16. <intent-filter>
  17. <action android:name="android.intent.action.MAIN" />
  18. <category android:name="android.intent.category.LAUNCHER" />
  19. </intent-filter>
  20. </activity>
  21. </application>
  22.  
  23. </manifest>
实例 BatteryDemo 中 MainActivity.java 的具体实现代码如下:
  1. package introduction.android.batterydemo;
  2.  
  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.IntentFilter;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.os.Bundle;
  9. import android.widget.CompoundButton;
  10. import android.widget.TextView;
  11. import android.widget.ToggleButton;
  12.  
  13. import org.w3c.dom.Text;
  14.  
  15. public class MainActivity extends AppCompatActivity {
  16. private ToggleButton button;
  17. private TextView text;
  18. BroadcastReceiver receiver = null;
  19.  
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.activity_main);
  24. button = (ToggleButton) findViewById(R.id.button);
  25. text = (TextView) findViewById(R.id.text);
  26.  
  27. final BroadcastReceiver receiver = new BroadcastReceiver() {
  28. @Override
  29. public void onReceive(Context context, Intent intent) {
  30. String action = intent.getAction();
  31. if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
  32. int current = intent.getExtras().getInt("level");
  33. int total = intent.getExtras().getInt("scale");
  34. int value = current * 100 / total;
  35. text.setText("当前电量是" + value + "%" + "");
  36. }
  37. }
  38. };
  39. button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  40. @Override
  41. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  42. if (isChecked) {
  43. IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
  44. registerReceiver(receiver, filter);
  45. } else {
  46. unregisterReceiver(receiver);
  47. text.setText("");
  48.  
  49. }
  50. }
  51. });
  52. }
  53. }
其中,Intent.ACTION_BATTERY_CHANGED 为当电池电量变化时产生的 Intent 对象中携带的 Action 信息。

IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED) ;

用于确定当前 BroadcastReceiver 对象接收的 Intent 对象的类型。

下面是常用的方法:
  • registerReceiver(receiver, filter): 动态注册 receiver。
  • int current = intent.getExtras().getInt("level"): 获取当前电池的电量。
  • int total=intent.getExtras().getInt("scale"): 获取总电量。
  • unregisterReceiver(receiver):注销 receiver 注册。

该应用程序若要使用静态注册,则需要在 AndroidManifest.xml 文件中添加如下代码:
  1. <receiver android:name="receiver">
  2. <intent-filter>
  3. <action android:name="android.intent.action.BATTERY_CHANGED"/>
  4. </intent-filter>
  5. </receiver>