上面讲述的广播接收器都是静态方式注册的,那么如何动态注册广播接收器呢?

  • 马上学Android 028 - BroadcastReceiver的使用教程03 - 动态注册广播监听电池信息

首先还是要创建一个广播接收器的类继承自BroadcastReceiver。

在Activity中,如果需要动态注册广播官方文档建议在onResume()进行注册,在onPause()中取消注册。注册部分代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

@Override
protected void onResume() {
super.onResume();
//动态注册广播接收器
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BATTERY_ACTION); //自定义Action
registerReceiver(mBatteryBR,intentFilter);
}

@Override
protected void onPause() {
super.onPause();
//取消注册广播接收器
unregisterReceiver(mBatteryBR);
}

这里给出系统广播Intent.ACTION_BATTERY_CHANGED中所包含的信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
"present"    (boolean)      -是否使用电池
"level"      (int)    -电池剩余容量
"scale"     (int)    -电池最大值,通常为100
"icon-small"  (int)    -图标ID。
"voltage"     (int)    -电池的电压(伏特)
"temperature"  (int)    -电池的温度,0.1度单位。例如 表示197的时候,意思为19.7度。
"technology"  (String)    -电池类型,例如,Li-ion等等。
"plugged"    (int)   -充电方式:
"status"     (int)    -电池状态:
BatteryManager.BATTERY_STATUS_CHARGING:充电状态。
BatteryManager.BATTERY_STATUS_DISCHARGING:放电状态。
BatteryManager.BATTERY_STATUS_NOT_CHARGING:未充满。
BatteryManager.BATTERY_STATUS_FULL:充满电。
BatteryManager.BATTERY_STATUS_UNKNOWN:未知状态。

"health"     (int)    -健康状态:
BatteryManager.BATTERY_HEALTH_GOOD:状态良好。
BatteryManager.BATTERY_HEALTH_DEAD:电池没有电。
BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:电池电压过高。
BatteryManager.BATTERY_HEALTH_OVERHEAT:电池过热。
BatteryManager.BATTERY_HEALTH_UNKNOWN:未知状态。

有了上面电池改变广播的信息基础之后,就可以解析出想要的电池信息了,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

private BroadcastReceiver mBatteryBR = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//处理Intent传回的电池信息
if(intent.getAction().equals(BATTERY_ACTION)){

int status = intent.getIntExtra("status", 0);
int health = intent.getIntExtra("health", 1);
boolean present = intent.getBooleanExtra("present", false);
int level = intent.getIntExtra("level", 0);
int scale = intent.getIntExtra("scale", 0);
int plugged = intent.getIntExtra("plugged", 0);
int voltage = intent.getIntExtra("voltage", 0);
int temperature = intent.getIntExtra("temperature", 0);
String technology = intent.getStringExtra("technology");

String statusString = "未知状态";
//电池状态的判断
switch(status) {
case BatteryManager.BATTERY_STATUS_UNKNOWN:
statusString = "状态未知";
break;
case BatteryManager.BATTERY_STATUS_CHARGING:
statusString = "充电状态";
break;
case BatteryManager.BATTERY_STATUS_DISCHARGING:
statusString = "放点状态";
break;
case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
statusString = "未充电";
break;
case BatteryManager.BATTERY_STATUS_FULL:
statusString = "充满电";
break;
}

String healthString = "未知状态";
//电池健康状态判断
switch (health) {
case BatteryManager.BATTERY_HEALTH_UNKNOWN:
healthString = "未知状态";
break;
case BatteryManager.BATTERY_HEALTH_GOOD:
healthString = "状态好";
break;
case BatteryManager.BATTERY_HEALTH_OVERHEAT:
healthString = "电池过热";
break;
case BatteryManager.BATTERY_HEALTH_DEAD:
healthString = "电池没有电";
break;
case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
healthString = "电池电压过高";
break;
case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
healthString = "未知错误";
break;
}

String acString = "未知状态";
//电池充电状态判断
switch (plugged) {
case BatteryManager.BATTERY_PLUGGED_AC:
acString = "直流充电";
break;
case BatteryManager.BATTERY_PLUGGED_USB:
acString = "USB充电";
break;
}
//输出电池状态信息
tvBatteryInfo.setText( "电池状态信息如下:\n"+
"\n是否使用电池: " + String.valueOf(present)+
"\n电池状态: " + statusString +
"\n电池电量: " + String.valueOf(level) + "%" +
"\n电池健康状态: " + healthString +
"\n最大值: " + String.valueOf(scale)+
"\n充电方式: " + acString +
"\n电池电压: " + String.valueOf(voltage) +
"\n电池温度: " + String.valueOf(temperature) +
"\n电池类型: " + technology);
}
}
};

动态注册广播更加灵活,但是如果是要监听一些特殊广播如:开机启动广播,那么就得使用静态方式注册。