ApiDemos 中的 getData 方法,用来获得要在界面上显示的 list,数据通过 PackageManager 从 Manifest 中获取。
不写字了,写了一晚上好累。。
这是我自己写的注释,可能有错误,请指正~
- /**
- * 一个内部是 map 的 list,用来储存 intent 以及他的名称
- */
- List<Map<String, Object>> myData = new ArrayList<Map<String, Object>>();
- /**
- * 获得 Manifest 的 Activity 标签里 intentfilter 中 action 为
- * {@code <code>android.intent.action.MAIN</code>} 的所有 Activity 中的信息
- */
- Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
- /**
- * 添加从 Manifest 中获得的信息的过滤条件【<category
- * android:name="android.intent.category.SAMPLE_CODE" />】
- */
- mainIntent.addCategory(Intent.CATEGORY_SAMPLE_CODE);
- PackageManager pm = getPackageManager();
- /**
- * intent 查寻条件,Activity 所配置的 action 和 category * flags:
- * MATCH_DEFAULT_ONLY :Category 必须带有 CATEGORY_DEFAULT 的 Activity,才匹配
- * * GET_INTENT_FILTERS :匹配 Intent 条件即可 * GET_RESOLVED_FILTER
- * :匹配 Intent 条件即可
- */
- List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);
- if (null == list)
- return myData;
- String[] prefixPath;
- String prefixWithSlash = prefix;
- if (prefix.equals("")) {
- /**
- * 首次运行,传入过来的地址为空字符串
- */
- prefixPath = null;
- } else {
- /**
- * 当点击 ttt 的时候 {@code <code>prefixPath 为 [ttt]</code>}
- * {@code <code>prefixWithSlash 为 ttt/</code>}
- */
- prefixPath = prefix.split("/");
- prefixWithSlash = prefix + "/";
- }
- int len = list.size();
- /**
- * 记录 key 值是否添加过
- */
- Map<String, Boolean> entries = new HashMap<String, Boolean>();
- for (int i = 0; i < len; i++) {
- /**
- * ResolveInfo 示例 {@code <code>ResolveInfo 41ec2f90
- * com.example.android.apis.app.HelloWorld p=0 o=0
- * m=0x108000}</code>}
- */
- ResolveInfo info = list.get(i);
- /**
- * 获取应用程序的名称
- */
- CharSequence labelSeq = info.loadLabel(pm);
- /**
- * 判断获得的程序的名称是不是空,如果是空的,那么获取程序的启动 Activity 的名称
- * 当我自己另外建立一个 Android 程序,其中建了一个 Activity,在 Manifest 中我给它设置属性为:
- * {@code <code><activity android:name="QueryMethodActivity" android:label="ttt/sss/xxx"></code>}
- * {@code <code> <intent-filter ></code>}
- * {@code <code> <action android:name="android.intent.action.MAIN" /></code>}
- * {@code <code> <category android:name="android.intent.category.SAMPLE_CODE" /></code>}
- * {@code <code> </intent-filter></code>}
- * {@code <code></activity></code>}
- * 那么在 ApiDemos 中运行的时候就会把它包括进去,并且是通过 info.loadLabel(pm) 获得的值。
- * 如果在 Activity 中 android
- * :label 有值,如上面的"ttt/sss/xxx"。那么通过 loadLabel 获得的值就是"ttt/sss/xxx"
- */
- String label = labelSeq != null ? labelSeq.toString() : info.activityInfo.name;
- /**
- * 以 s/t/x 为例,此时 prefixWithSlash 为空字符串,prefixPath 为空
- */
- if (prefixWithSlash.length() == 0 || label.startsWith(prefixWithSlash)) {
- /**
- * labelPath 中为 t s x
- */
- String[] labelPath = label.split("/");
- /**
- * nextLabel 为 t
- */
- String nextLabel = prefixPath == null ? labelPath[0] : labelPath[prefixPath.length];
- /**
- * @param 第一次
- * {@code <code>(prefixPath != null ? prefixPath.length : 0) == labelPath.length - 1</code>}
- * 表达式的值为 false
- * 进入到 else,entries 中没有当前值,为空,然后添加到结果 list(myData
- * ) 以及添加到记录 map(entries)
- * @param 点击到 sss 之后
- * {@code <code> 此时 prefixPath 的值为 [ttt, sss]</code>}
- * {@code <code>myData 为 []</code>}
- * {@code <code>nextLabel 为 xxx</code>}
- * {@code <code> info.activityInfo.applicationInfo.packageName 为 com.example.test_queryintentactivitiesmethod</code>}
- * {@code <code>info.activityInfo.name 为 com.example.test_queryintentactivitiesmethod.QueryMethodActivity</code>}
- * 然后
- */
- if ((prefixPath != null ? prefixPath.length : 0) == labelPath.length - 1) {
- /**
- * 最后的层级,点击这个进去的就是实实在在的 Activity 了,也就是"文件"
- */
- addItem(myData, nextLabel, activityIntent(info.activityInfo.applicationInfo.packageName, info.activityInfo.name));
- } else {
- if (entries.get(nextLabel) == null) {
- /**
- * 将 name 以及 Intent 放到 myData 中 这个添加的只是"目录"
- */
- addItem(myData, nextLabel, browseIntent(prefix.equals("") ? nextLabel : prefix + "/" + nextLabel));
- /**
- * 记录已经添加了哪个值
- */
- entries.put(nextLabel, true);
- }
- }
- }
- }
- //排序
- Collections.sort(myData, sDisplayNameComparator);
- return myData;
- }
转载请注明:热爱改变生活.cn » ApiDemos 中 getData 方法的学习
本博客只要没有注明“转”,那么均为原创。 转载请注明链接:sumile.cn » ApiDemos 中 getData 方法的学习