对Android近期任务列表(Recent Applications)的简单分析
转载自:http://www.cnblogs.com/coding-way/archive/2013/06/05/3118732.html
这里的近期任务列表就是长按Home键出来的那个Dialog,里面放着近期打开过的应用,当然3.0以上系统的多任务切换键也是。
这个Dialog的实现在Android源码的/frameworks/base/policy/src/com/android/internal/policy/impl/RecentApplicationsDialog.java中。
接下来就对这个源码分析一下。
先把整个源码贴出来:
从源码可以看出,关键部分有三处。
一个很关键的内部类:
// 每个任务都包含一个Tag,这个Tag保存着这个App的一些非常有用的信息 class RecentTag { ActivityManager.RecentTaskInfo info; Intent intent; }
这个RecentTag保存在每个近期任务的图标里,并且保存着这个任务的原始信息。
刚启动Dialog时对每个任务的初始化:
1 private void reloadButtons() { 2 3 final Context context = getContext(); 4 final PackageManager pm = context.getPackageManager(); 5 final ActivityManager am = (ActivityManager) 6 context.getSystemService(Context.ACTIVITY_SERVICE); 7 8 //拿到最近使用的应用的信息列表 9 final List<ActivityManager.RecentTaskInfo> recentTasks = 10 am.getRecentTasks(MAX_RECENT_TASKS, ActivityManager.RECENT_IGNORE_UNAVAILABLE); 11 12 //自制一个home activity info,用来区分 13 ActivityInfo homeInfo = 14 new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME) 15 .resolveActivityInfo(pm, 0); 16 17 IconUtilities iconUtilities = new IconUtilities(getContext()); 18 19 int index = 0; 20 int numTasks = recentTasks.size(); 21 //开始初始化每个任务的信息 22 for (int i = 0; i < numTasks && (index < NUM_BUTTONS); ++i) { 23 final ActivityManager.RecentTaskInfo info = recentTasks.get(i); 24 25 //复制一个任务的原始Intent 26 Intent intent = new Intent(info.baseIntent); 27 if (info.origActivity != null) { 28 intent.setComponent(info.origActivity); 29 } 30 31 //跳过home activity 32 if (homeInfo != null) { 33 if (homeInfo.packageName.equals( 34 intent.getComponent().getPackageName()) 35 && homeInfo.name.equals( 36 intent.getComponent().getClassName())) { 37 continue; 38 } 39 } 40 41 intent.setFlags((intent.getFlags()&~Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) 42 | Intent.FLAG_ACTIVITY_NEW_TASK); 43 final ResolveInfo resolveInfo = pm.resolveActivity(intent, 0); 44 if (resolveInfo != null) { 45 final ActivityInfo activityInfo = resolveInfo.activityInfo; 46 final String title = activityInfo.loadLabel(pm).toString(); 47 Drawable icon = activityInfo.loadIcon(pm); 48 49 if (title != null && title.length() > 0 && icon != null) { 50 final TextView tv = mIcons[index]; 51 tv.setText(title); 52 icon = iconUtilities.createIconDrawable(icon); 53 tv.setCompoundDrawables(null, icon, null, null); 54 //new一个Tag,保存这个任务的RecentTaskInfo和Intent 55 RecentTag tag = new RecentTag(); 56 tag.info = info; 57 tag.intent = intent; 58 tv.setTag(tag); 59 tv.setVisibility(View.VISIBLE); 60 tv.setPressed(false); 61 tv.clearFocus(); 62 ++index; 63 } 64 } 65 } 66 67 ...//无关紧要的代码 68 }
这里的过程是:先用ActivityManager获取RecentTasks并生成一个List,然后利用这个List为Dialog中的每个任务初始化,并生成对应的信息RecentTag。
需要注意的是,RecentTag中的Intent是从对应任务的原始Intent复制过来的,这意味着那个原始Intent的一些Extra参数将会一并复制过来,
我来举个例子:比如我的App支持从第三方启动,并且第三方要提供一个token,当然这个token就以Extra参数的形式放进Intent里,然后通过startActivity()启动我的App;然后我的App根据这个token来处理,注意这里,当我的App退出后,再从近期任务里启动这个App,之前的那个token还会传递给我的App,这里就会出现错误了,原因就是上面分析的。这就是为什么从第三方跳转的应用不会出现在近期任务的列表里(比如点击短信里的url启动一个浏览器,之后近期任务里只有短信app,不会出现浏览器app)。要想不出现在近期任务里,可以给Intent设置FLAG_ACTIVITY_NO_HISTORY标志。
响应每个任务的点击事件:
1 private void switchTo(RecentTag tag) { 2 if (tag.info.id >= 0) { 3 // 这个Task没有退出,直接移动到前台 4 final ActivityManager am = (ActivityManager) 5 getContext().getSystemService(Context.ACTIVITY_SERVICE); 6 am.moveTaskToFront(tag.info.id, ActivityManager.MOVE_TASK_WITH_HOME); 7 } else if (tag.intent != null) { 8 //task退出了的话,id为-1,则使用RecentTag中的Intent重新启动 9 tag.intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY 10 | Intent.FLAG_ACTIVITY_TASK_ON_HOME); 11 try { 12 getContext().startActivity(tag.intent); 13 } catch (ActivityNotFoundException e) { 14 Log.w("Recent", "Unable to launch recent task", e); 15 } 16 } 17 }
如果该Task没有退出,只是切到后台,则切换到前台;如果已经退出,就要重新启动了。
这里的Intent就是之前说的,重复使用的旧Intent了,这里注意,系统添加了FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY和FLAG_ACTIVITY_TASK_ON_HOME标志,所以我们可以在App中通过判断Intent的flag是否包含这两个来判断是否是从近期任务里启动的。注意FLAG_ACTIVITY_TASK_ON_HOME标志是Api 11添加的,所以11一下的之判断FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY就行了。