Broadcast动态注册与静态注册
动态注册
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// 在onCreate()中注册registerBatteryReceiver();
private void registerBatteryReceiver() {
// 收听的频道是 电量变化
IntentFilter intentFilter = new IntentFilter();
// 设置频道
intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
// 创建一个广播接收者
mBatteryLevelReceiver = new BatteryLevelReceiver();
// 注册
this.registerReceiver(mBatteryLevelReceiver, intentFilter);
}
/**
* 创建一个广播接收者
*/
private class BatteryLevelReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
Log.d(TAG, "Battery change action is == " + action);
int currentLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
Log.d(TAG, "当前电量:" + currentLevel);
if (mBatteryLevelText != null) {
mBatteryLevelText.setText("当前电量:" + currentLevel);
}
int maxLevel = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);
float percent = currentLevel * 1.0f / maxLevel * 100;
Log.d(TAG, "当前电量百分比为:" + percent + "%");
}
}
}
// 在onDestroy()中取消广播注册
protected void onDestroy() {
super.onDestroy();
// 取消广播注册
if (mBatteryLevelReceiver != null) {
this.unregisterReceiver(mBatteryLevelReceiver);
}
}静态注册
1
2
3
4
5
6
7
8
9
10
11// 在AndroidManifest.xml对静态广播进行注册
<receiver android:name=".AppStateChangeReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
// 其余与动态类似,但无需在onCreate()与onDestroy()注册与销毁,仅需设置广播接收者即可
自定义Broadcast广播与接收
1 | // 例如本次在按钮中设置发送广播,仅在上述部分多添加一个发送intent并设置发送频道与内容即可 |
有序广播
上述部分为无序广播,即任何应用只要有对应的action就可以接收。(Android8.0后静态注册无法有序,需用动态注册)
有序广播的特点:有序、可以终止向下传达、可以修改广播的内容。