am命令发送广播:
am broadcast -a .BOOT_COMPLETED
就发送了一条开机广播,其他广播类似,其基本格式为:
am broadcast -a 你的广播
同样,在开发过程中,有时候想知道我们自己定义的广播是否成功发送,或者是某些情况触发了系统的什么广播,那么下文告诉各位一个小技巧:
命令行执行:
dumpsys | grep BroadcastRecord
结果如下所示:
BroadcastRecord{114315e u0 } to user 0
BroadcastRecord{452e20c u0 .JOB_DELAY_EXPIRED} to user 0
BroadcastRecord{9721755 u0 .JOB_DEADLINE_EXPIRED} to user 0
BroadcastRecord{96e056a u0 } to user 0
BroadcastRecord{37aca5b u0 .NETWORK_STATS_POLL} to user 0
BroadcastRecord{25212f8 u-1 .NETWORK_STATS_UPDATED} to user -1
BroadcastRecord{289d3d1 u0 } to user 0
BroadcastRecord{411b137 u0 } to user 0
BroadcastRecord{71166a4 u0 } to user 0
BroadcastRecord{d85d7c2 u0 .JOB_DELAY_EXPIRED} to user 0
BroadcastRecord{bc629d3 u0 .JOB_DEADLINE_EXPIRED} to user 0
BroadcastRecord{504910 u0 .ACTIVATE_MEDIAPROCESS} to user 0
BroadcastRecord{cddae0e u0 } to user 0
BroadcastRecord{23f902f u0 } to user 0
BroadcastRecord{8687fc5 u0 } to user 0
BroadcastRecord{83f004b u0 } to user 0
BroadcastRecord{9ec2a28 u0 .JOB_DELAY_EXPIRED} to user 0
BroadcastRecord{1565341 u0 .JOB_DEADLINE_EXPIRED} to user 0
BroadcastRecord{46570e6 u0 } to user 0
BroadcastRecord{4edc0d4 u0 } to user 0
BroadcastRecord{1f3e27d u0 } to user 0
从上到下,是按照从新到旧的顺序排列的,也就是说第一行是系统最新发出的一个广播。
通过命令行执行adb shell am broadcast发送广播通知。
adb shell am broadcast 后面的参数有:
[-a <ACTION>]
[-d <DATA_URI>]
[-t <MIME_TYPE>]
[-c <CATEGORY> [-c <CATEGORY>] ...]
[-e|--es <EXTRA_KEY> <EXTRA_STRING_VALUE> ...]
[--ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> ...]
[-e|--ei <EXTRA_KEY> <EXTRA_INT_VALUE> ...]
[-n <COMPONENT>]
[-f <FLAGS>] [<URI>]
例如:
adb shell am broadcast -a --es test_string "this is test string" --ei test_int 100 --ez test_boolean true
说明:红色为key,蓝色为value,分别为String类型,int类型,boolean类型