免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1354 | 回复: 0

Android110426: Intent机制实现注记 [复制链接]

论坛徽章:
0
发表于 2011-12-20 09:44 |显示全部楼层
Android110426: Intent机制实现注记


Email:    zcatt@163.com
Blog    http://zcatt.blog.chinaunix.net
 
内容提要
Android如何寻找intent受体.以供备忘和参考。

声明
仅限学习交流,禁止商业用途。转载需注明出处。

版本记录
Date        Ver        Note
2011-04-26    0.1        Draft.  zcatt, Beijing
 
Intent可以理解为一种异步通信机制. 发送者将目的地址和参数信息封装到Intent中发送出去. 而各个受体,包括activity, receiver和service, 使用intent-filter的方式告诉android系统自己可以处理的Intent种类.   android系统根据Intent的目的地址解析Intent的受体. Intent的目的地址可以分为两类, 一类是明确定义了ComponentName, 对于此类intent, android系统直接查找所需的受体就可, 不存在匹配过程. 另一类则是没有定义ComponentName, 但是确通过对action, category和data的定义给出匹配信息, android系统根据这些信息查找系统内已经登记的所有受体的intentFilter信息, 匹配出合适的intent受体, 如果受体是多个,则按照priority和preference挑选出最适合的intent受体.
Intent整个机制主要相关的类包括如下, 下面分述之.


  1. Intent

  2. IntentFilter
  3.     PackageParser.IntentInfo
  4.         PackageParser.ActivityIntentInfo
  5.         PackageParser.ServiceIntentInfo
  6.         PackageManagerService.PreferredActivity

  7. ResolveInfo

  8. IntentResolver
  9.     PackageManagerService.ActivityIntentResolver
  10.     PackageManagerService.ServiceIntentResolver


IntentFilter描述的是intent filter, 对应的是AndroidManifest.xml中的<intent-filter>. 主要的成员包括:

  1. private int mPriority;
  2. private final ArrayList<String> mActions;
  3. private ArrayList<String> mCategories = null;
  4. private ArrayList<String> mDataSchemes = null;
  5. private ArrayList<AuthorityEntry> mDataAuthorities = null;
  6. private ArrayList<PatternMatcher> mDataPaths = null;
  7. private ArrayList<String> mDataTypes = null;
  8. private boolean mHasPartialTypes = false;


关键的方法是

  1. public final int match(String action, String type, String scheme,
  2.             Uri data, Set<String> categories, String logTag)

IntentFilter的功用很简单, 就是判断(action, type, scheme, data, categories)是否匹配自己描述的intentFilter.


IntentResolver则是一个框架类, 可以将多个intentFilter组织起来. 输入intent, 遍历这些intentFilter,得到适合的intentFilter.

  1. public class IntentResolver<F extends IntentFilter, R extends Object>

其中F是intentFilter的类, R则是匹配intent后所得到的结果, 是关于适合intentFilter的信息.IntentResolver派生出两个子类PackageManagerService.ActivityIntentResolver和PackageManagerService.ServiceIntentResolver. 前者对应的是<activity>和<receiver>相关的intentFilter, 后者对应的是<service>相关的intentFilter.

  1. private final class ActivityIntentResolver
  2.       extends IntentResolver<PackageParser.ActivityIntentInfo, ResolveInfo>

  3. private final class ServiceIntentResolver
  4.       extends IntentResolver<PackageParser.ServiceIntentInfo, ResolveInfo>



IntentResolver重要的方法有3个

  1. public List<R> queryIntent(Intent intent, String resolvedType, boolean defaultOnly)

  2. protected R newResult(F filter, int match)

  3. protected boolean allowFilterResult(F filter, List<R> dest)

  4. protected void sortResults(List<R> results)
 

queryIntent()返回的是已经按照匹配程度排序的intent匹配结果列表, 实际就是适合输入参数intent的intentFilter的结果列表.在这个query过程中newResult(), allowFilterResult()和sortResults()会被queryIntent()调用, 而这个3个方法也通常是子类要考虑实现的.
newResult()用于将匹配的filter封装到R中.
allowFilterResult()可以控制筛选, 返回false的则会从结果dest中剔除, 通常allowFilterResult()被IntentResolver的子类用于防止dest中的重复添加相同filter, 如果dest中已经含有filter,则返回false.
sortResults()则对结果进行排序, 可以看到IntentResolver的排序规则是根据priority. 而ActivityIntentResolver和ServiceIntentResolver则复杂些, 还考虑了preferredOrder和intentFilter是否是default的情况, 细节参见PackageManagerService.mResolvePrioritySorter.


ActivityIntentInfo和ServiceIntentInfo是PackageParser解析xml, 容纳intentFilter描述信息的类.

  1. public static class IntentInfo extends IntentFilter

  2. public final static class ActivityIntentInfo extends IntentInfo

  3. public final static class ServiceIntentInfo extends IntentInfo


整个intent的解析场景是这样的, PackageManangerService调用PackageParser装载所有的filterIntent到mActivities, mReceivers和mServices. 这些信息将在query intent是被使用.

  1. // All available activities, for your resolving pleasure.
  2. final ActivityIntentResolver mActivities =
  3.         new ActivityIntentResolver();

  4. // All available receivers, for your resolving pleasure.
  5. final ActivityIntentResolver mReceivers =
  6.         new ActivityIntentResolver();

  7. // All available services, for your resolving pleasure.
  8. final ServiceIntentResolver mServices = new ServiceIntentResolver();


PackageManangerService提供以下的几个方法做为Android系统对intent机制的根本支持.

  1. public List<ResolveInfo> queryIntentActivities(Intent intent,
  2.         String resolvedType, int flags)

  3. public List<ResolveInfo> queryIntentActivityOptions(ComponentName caller,
  4.         Intent[] specifics, String[] specificTypes, Intent intent,
  5.         String resolvedType, int flags)

  6. public List<ResolveInfo> queryIntentReceivers(Intent intent,
  7.         String resolvedType, int flags)

  8. public List<ResolveInfo> queryIntentServices(Intent intent,
  9.         String resolvedType, int flags)

Locations of visitors to this page



您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP