ApiDemos 中 getData 方法的学习 – 热爱改变生活
我的GitHub GitHub |     登录
  • If you can't fly, then run; if you can't run, then walk; if you can't walk, then crawl
  • but whatever you do, you have to keep moving forward。
  • “你骗得了我有什么用,这是你自己的人生”
  • 曾有伤心之地,入梦如听 此歌

ApiDemos 中 getData 方法的学习

Android sinvader 4437℃ 0评论

ApiDemos 中的 getData 方法,用来获得要在界面上显示的 list,数据通过 PackageManager 从 Manifest 中获取。
不写字了,写了一晚上好累。。
这是我自己写的注释,可能有错误,请指正~

  1. /**
  2. * 一个内部是 map 的 list,用来储存 intent 以及他的名称
  3. */
  4. List<Map<String, Object>> myData = new ArrayList<Map<String, Object>>();
  5. /**
  6. * 获得 Manifest 的 Activity 标签里 intentfilter 中 action 为
  7. * {@code <code>android.intent.action.MAIN</code>} 的所有 Activity 中的信息
  8. */
  9. Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
  10. /**
  11. * 添加从 Manifest 中获得的信息的过滤条件【<category
  12. * android:name="android.intent.category.SAMPLE_CODE" />】
  13. */
  14. mainIntent.addCategory(Intent.CATEGORY_SAMPLE_CODE);
  15.  
  16. PackageManager pm = getPackageManager();
  17. /**
  18. * intent 查寻条件,Activity 所配置的 action 和 category    * flags:
  19. * MATCH_DEFAULT_ONLY :Category 必须带有 CATEGORY_DEFAULT 的 Activity,才匹配
  20. *  *  GET_INTENT_FILTERS :匹配 Intent 条件即可       *  GET_RESOLVED_FILTER
  21. * :匹配 Intent 条件即可
  22. */
  23. List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);
  24.  
  25. if (null == list)
  26. return myData;
  27.  
  28. String[] prefixPath;
  29. String prefixWithSlash = prefix;
  30.  
  31. if (prefix.equals("")) {
  32. /**
  33. * 首次运行,传入过来的地址为空字符串
  34. */
  35. prefixPath = null;
  36. } else {
  37. /**
  38. * 当点击 ttt 的时候 {@code <code>prefixPath 为 [ttt]</code>}
  39. * {@code <code>prefixWithSlash 为 ttt/</code>}
  40. */
  41. prefixPath = prefix.split("/");
  42. prefixWithSlash = prefix + "/";
  43. }
  44.  
  45. int len = list.size();
  46. /**
  47. * 记录 key 值是否添加过
  48. */
  49. Map<String, Boolean> entries = new HashMap<String, Boolean>();
  50.  
  51. for (int i = 0; i < len; i++) {
  52. /**
  53. * ResolveInfo 示例 {@code <code>ResolveInfo 41ec2f90
  54. * com.example.android.apis.app.HelloWorld p=0 o=0
  55. * m=0x108000}</code>}
  56. */
  57. ResolveInfo info = list.get(i);
  58. /**
  59. * 获取应用程序的名称
  60. */
  61. CharSequence labelSeq = info.loadLabel(pm);
  62. /**
  63. * 判断获得的程序的名称是不是空,如果是空的,那么获取程序的启动 Activity 的名称
  64. * 当我自己另外建立一个 Android 程序,其中建了一个 Activity,在 Manifest 中我给它设置属性为:
  65. * {@code <code><activity android:name="QueryMethodActivity" android:label="ttt/sss/xxx"></code>}
  66. * {@code <code>  <intent-filter ></code>}
  67. * {@code <code>     <action android:name="android.intent.action.MAIN" /></code>}
  68. * {@code <code>     <category android:name="android.intent.category.SAMPLE_CODE" /></code>}
  69. * {@code <code>  </intent-filter></code>}
  70. * {@code <code></activity></code>}
  71. * 那么在 ApiDemos 中运行的时候就会把它包括进去,并且是通过 info.loadLabel(pm) 获得的值。
  72. * 如果在 Activity 中 android
  73. * :label 有值,如上面的"ttt/sss/xxx"。那么通过 loadLabel 获得的值就是"ttt/sss/xxx"
  74. */
  75. String label = labelSeq != null ? labelSeq.toString() : info.activityInfo.name;
  76. /**
  77. * 以 s/t/x 为例,此时 prefixWithSlash 为空字符串,prefixPath 为空
  78. */
  79. if (prefixWithSlash.length() == 0 || label.startsWith(prefixWithSlash)) {
  80. /**
  81. * labelPath 中为 t s x
  82. */
  83. String[] labelPath = label.split("/");
  84. /**
  85. * nextLabel 为 t
  86. */
  87. String nextLabel = prefixPath == null ? labelPath[0] : labelPath[prefixPath.length];
  88. /**
  89. * @param 第一次
  90. *            {@code <code>(prefixPath != null ? prefixPath.length : 0) == labelPath.length - 1</code>}
  91. *             表达式的值为 false
  92. *             进入到 else,entries 中没有当前值,为空,然后添加到结果 list(myData
  93. *            ) 以及添加到记录 map(entries)
  94. * @param 点击到 sss 之后
  95. *            {@code <code> 此时 prefixPath 的值为 [ttt, sss]</code>}
  96. *            {@code <code>myData 为 []</code>}
  97. *            {@code <code>nextLabel 为 xxx</code>}
  98. *            {@code <code> info.activityInfo.applicationInfo.packageName 为 com.example.test_queryintentactivitiesmethod</code>}
  99. *            {@code <code>info.activityInfo.name 为 com.example.test_queryintentactivitiesmethod.QueryMethodActivity</code>}
  100. *             然后
  101. */
  102. if ((prefixPath != null ? prefixPath.length : 0) == labelPath.length - 1) {
  103. /**
  104. * 最后的层级,点击这个进去的就是实实在在的 Activity 了,也就是"文件"
  105. */
  106. addItem(myData, nextLabel, activityIntent(info.activityInfo.applicationInfo.packageName, info.activityInfo.name));
  107. } else {
  108. if (entries.get(nextLabel) == null) {
  109. /**
  110. * 将 name 以及 Intent 放到 myData 中 这个添加的只是"目录"
  111. */
  112. addItem(myData, nextLabel, browseIntent(prefix.equals("") ? nextLabel : prefix + "/" + nextLabel));
  113. /**
  114. * 记录已经添加了哪个值
  115. */
  116. entries.put(nextLabel, true);
  117. }
  118. }
  119. }
  120. }
  121. //排序
  122. Collections.sort(myData, sDisplayNameComparator);
  123.  
  124. return myData;
  125. }
  126.  
getData
¥ 有帮助么?打赏一下~

转载请注明:热爱改变生活.cn » ApiDemos 中 getData 方法的学习


本博客只要没有注明“转”,那么均为原创。 转载请注明链接:sumile.cn » ApiDemos 中 getData 方法的学习

喜欢 (2)
发表我的评论
取消评论
表情

如需邮件形式接收回复,请注册登录

Hi,你需要填写昵称和邮箱~

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址