首页
社区
课程
招聘
[原创]三行代码获取特定广播的所有接收者
2012-9-26 15:20 28496

[原创]三行代码获取特定广播的所有接收者

2012-9-26 15:20
28496
Android中收到短信等事件都是通过广播发送给应用程序的,360手机卫士等程序都是通过注
册高优先级的BroadcastReceiver来实现短信防火墙等功能。对于我们来说很想知道系统中都有
哪些程序注册了BroadcastReceiver,但是通过什么方法能获取系统BroadcastReceiver的列
表呢?

      我在群里问了一下,他们告诉我的答案居然是分析所有apk的AndroidManifest.xml,再加上
解析dex里面的动态注册!而这根本不是我想达到的目的,我要知道的是目前系统中实时的
BroadcastReceiver,而不是通过静态分析得到。于是我开始看Android Framework代码,想
搞清楚广播的实现机制到底是怎样的。

      注册BroadcastReceiver的过程是这样的:Activity调用registerReceiver,然后经过几层
内部类接口的调用之后,通过Binder机制与ActivityManagerService通信,而
ActivityManagerService里有一个ReceiverList保存着系统所有的BroadcastReceiver。

      发送广播的过程是:Activity向ActivityManagerService发送广播,
ActivityManagerService查找ReceiverList,通过比对IntentFilter找到所有对应的
BroadcastReceiver,根据BroadcastReceiver的优先级进行排序后,扔进广播发送队列里。
而后由专门的线程负责投递广播消息。


      大致的过程就是这样的,那么目标就是获取ActivityManagerService中的ReceiverList。
一开始我想的办法是:因为ActivityManagerService是一个单独的进程(system_server),
我们可以通过注入system_server进程来获得ReceiverList。但是由于
ActivityManagerService是java实现的,无法直接获得ReceiverList,要解析java的数据结构
难度就太大了。
      于是又再次查看
frameworks/base/services/java/com/android/server/am/ActivityManagerService.java:
public final class ActivityManagerService extends ActivityManagerNative
		implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {
	......

	private final int broadcastIntentLocked(ProcessRecord callerApp,
			String callerPackage, Intent intent, String resolvedType,
			IIntentReceiver resultTo, int resultCode, String resultData,
			Bundle map, String requiredPermission,
			boolean ordered, boolean sticky, int callingPid, int callingUid) {
		intent = new Intent(intent);

		......

		// Figure out who all will receive this broadcast.
		List receivers = null;
		List<BroadcastFilter> registeredReceivers = null;
		try {
			if (intent.getComponent() != null) {
				......
			} else {
				......
				registeredReceivers = mReceiverResolver.queryIntent(intent, resolvedType, false);
			}
		} catch (RemoteException ex) {
			......
		}

		final boolean replacePending =
			(intent.getFlags()&Intent.FLAG_RECEIVER_REPLACE_PENDING) != 0;

		int NR = registeredReceivers != null ? registeredReceivers.size() : 0;
		if (!ordered && NR > 0) {
			// If we are not serializing this broadcast, then send the
			// registered receivers separately so they don't wait for the
			// components to be launched.
			BroadcastRecord r = new BroadcastRecord(intent, callerApp,
				callerPackage, callingPid, callingUid, requiredPermission,
				registeredReceivers, resultTo, resultCode, resultData, map,
				ordered, sticky, false);
			......
			boolean replaced = false;
			if (replacePending) {
				for (int i=mParallelBroadcasts.size()-1; i>=0; i--) {
					if (intent.filterEquals(mParallelBroadcasts.get(i).intent)) {
						......
						mParallelBroadcasts.set(i, r);
						replaced = true;
						break;
					}
				}
			}

			if (!replaced) {
				mParallelBroadcasts.add(r);

				scheduleBroadcastsLocked();
			}

			registeredReceivers = null;
			NR = 0;
		}

		......

	}

	......
}


      看这一句:
registeredReceivers = mReceiverResolver.queryIntent(intent, resolvedType, false); 
到frameworks\base\services\java\com\android\server\IntentReslover.java中,注意到一个特性:
public List<R> queryIntent(Intent intent, String resolvedType, boolean defaultOnly) {
        String scheme = intent.getScheme();

        ArrayList<R> finalList = new ArrayList<R>();

        final boolean debug = localLOGV ||
                ((intent.getFlags() & Intent.FLAG_DEBUG_LOG_RESOLUTION) != 0);

        if (debug) Slog.v(
            TAG, "Resolving type " + resolvedType + " scheme " + scheme
            + " of intent " + intent);
       sortResults(finalList);
............
        if (debug) {
            Slog.v(TAG, "Final result list:");
            for (R r : finalList) {
                Slog.v(TAG, "  " + r);
            }
        }
        return finalList;
    }


      这个有个debug选项可以打印出最终获得的ReceiverList,还是按照优先级排过序的有木有?

      于是乎获取接收器列表就太简单了,只需要三行代码即可:

    	Intent intent = new Intent("android.provider.Telephony.SMS_RECEIVED");
    	intent.addFlags(Intent.FLAG_DEBUG_LOG_RESOLUTION);
        sendBroadcast(intent);


      重点就是第二句,设置了FLAG_DEBUG_LOG_RESOLUTION,这样就会在LogCat中打印
出所有注册了android.provider.Telephony.SMS_RECEIVED的BroadcastReceiver。用
IntentResolver作为TAG过滤一下看起来更方便:


[培训]《安卓高级研修班(网课)》月薪三万计划,掌握调试、分析还原ollvm、vmp的方法,定制art虚拟机自动化脱壳的方法

收藏
点赞3
打赏
分享
最新回复 (24)
雪    币: 118
活跃值: (106)
能力值: ( LV6,RANK:90 )
在线值:
发帖
回帖
粉丝
pc小波 1 2012-9-26 15:42
2
0
顶一个!!!
雪    币: 279
活跃值: (160)
能力值: ( LV9,RANK:170 )
在线值:
发帖
回帖
粉丝
pentacleNC 4 2012-9-26 15:42
3
0
没抢到沙发~
雪    币: 19
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
oah球 2012-9-26 15:50
4
0
雪    币: 4581
活跃值: (942)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
b23526 2012-9-26 16:23
5
0
好帖,必须支持
雪    币: 118
活跃值: (72)
能力值: ( LV3,RANK:30 )
在线值:
发帖
回帖
粉丝
ruoko 2012-9-26 16:42
6
0
顶一个,楼主好帅,我这儿才刚刚Android起步。
雪    币: 89
活跃值: (53)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
swlilike 2012-9-27 09:04
7
0
安全中心众多成员中马上要新增一人拉, android正在发芽
雪    币: 73
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
kingcomer 2012-9-27 13:03
8
0
额。。。现在都搞android去了~~
雪    币: 2175
活跃值: (966)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
Crakme 2012-9-29 09:27
9
0
果然还是要多研究源码
文章中的图片看不到  不知道是我网络问题 还是怎么回事
雪    币: 78
活跃值: (85)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
baixinye 1 2012-9-29 12:41
10
0
我是来膜拜xfocus创始人之一的~
雪    币: 162
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
upxshell 2012-10-30 09:42
11
0
不是新人呀,isno是最早的黑客大牛之一,这个应该就是那个吧。我QQ里边还有他。
雪    币: 122
活跃值: (45)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
figofuture 2012-11-14 13:57
12
0
这个在android 4.0+还有效么?
雪    币: 239
活跃值: (62)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
beactived 2012-11-25 14:51
13
0
牛人,找了很长时间如何获取动态注册的Receiver,原来几行代码就可以搞定了!
雪    币: 6192
活跃值: (2080)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
shuichon 2012-11-25 15:03
14
0
很不错的帖子,分析的很透彻。相当不错。收录。
雪    币: 239
活跃值: (62)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
beactived 2013-7-2 23:31
15
0
4.1.2版本就不允许读log了,还有什么别的方法可以获取广播的接收顺序吗?
雪    币: 224
活跃值: (16)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
zhoujiamur 1 2013-7-15 09:50
16
0
分析的不错
雪    币: 0
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
maixiaokou 2013-7-15 10:06
17
0
顶,好巧妙的方法啊。
雪    币: 210
活跃值: (507)
能力值: ( LV4,RANK:40 )
在线值:
发帖
回帖
粉丝
anywhere杨 2013-7-15 10:37
18
0
...强大了。
雪    币: 43
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
KILATER 2013-7-21 21:03
19
0
好帖,必须支持
雪    币: 37
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
aopicwf 2014-3-3 12:40
20
0
感觉framework的代码量太多,还需要耐心啊
雪    币: 131
活跃值: (98)
能力值: ( LV5,RANK:60 )
在线值:
发帖
回帖
粉丝
gamehacker 1 2014-3-3 12:44
21
0
我是来膜拜楼主的…………
雪    币: 37
活跃值: (22)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
pboy 2014-3-3 15:58
22
0
膜拜~
雪    币: 7
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
dgsj 2014-3-24 16:40
23
0
可惜新版本失效了
雪    币: 171
活跃值: (804)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
victorkong 2014-3-26 09:43
24
0
楼主好牛,很有帮助
雪    币: 250
活跃值: (251)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
OnlyEnd 2014-3-27 16:25
25
0
分析得很好啊。。。学习了
游客
登录 | 注册 方可回帖
返回