对Android近期任务列表(Recent Applications)的简单分析

对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中。

接下来就对这个源码分析一下。

先把整个源码贴出来:

  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就行了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单Android 11近期任务(Recent Tasks)的示例代码: 1. 首先,在你的AndroidManifest.xml文件中添加以下权限: ```xml <uses-permission android:name="android.permission.MANAGE_ACTIVITY_STACKS" /> ``` 2. 创建一个新的Activity,例如`RecentTasksActivity`,并在其布局文件中添加一个显示近期任务列表的RecyclerView: ```xml <!-- recent_tasks_activity.xml --> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 3. 在`RecentTasksActivity`中,通过`ActivityManager`获取近期任务列表: ```java public class RecentTasksActivity extends AppCompatActivity { private RecyclerView recyclerView; private RecentTasksAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.recent_tasks_activity); recyclerView = findViewById(R.id.recyclerView); recyclerView.setLayoutManager(new LinearLayoutManager(this)); ActivityManager activityManager = getSystemService(ActivityManager.class); List<ActivityManager.AppTask> recentTasks = activityManager.getAppTasks(); adapter = new RecentTasksAdapter(recentTasks); recyclerView.setAdapter(adapter); } } ``` 4. 创建一个RecyclerView的适配器`RecentTasksAdapter`来显示近期任务列表: ```java public class RecentTasksAdapter extends RecyclerView.Adapter<RecentTasksAdapter.TaskViewHolder> { private List<ActivityManager.AppTask> tasks; public RecentTasksAdapter(List<ActivityManager.AppTask> tasks) { this.tasks = tasks; } @NonNull @Override public TaskViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.task_item, parent, false); return new TaskViewHolder(view); } @Override public void onBindViewHolder(@NonNull TaskViewHolder holder, int position) { ActivityManager.AppTask task = tasks.get(position); ActivityManager.RecentTaskInfo info = task.getTaskInfo(); holder.taskLabel.setText(info.baseActivity.getPackageName()); holder.taskDescription.setText(info.description); } @Override public int getItemCount() { return tasks.size(); } static class TaskViewHolder extends RecyclerView.ViewHolder { TextView taskLabel; TextView taskDescription; public TaskViewHolder(@NonNull View itemView) { super(itemView); taskLabel = itemView.findViewById(R.id.taskLabel); taskDescription = itemView.findViewById(R.id.taskDescription); } } } ``` 5. 创建一个列表项的布局文件`task_item.xml`用于显示近期任务信息: ```xml <!-- task_item.xml --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="16dp"> <TextView android:id="@+id/taskLabel" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="16sp" android:textStyle="bold" /> <TextView android:id="@+id/taskDescription" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="14sp" /> </LinearLayout> ``` 以上代码演示了如何获取近期任务列表,并在RecyclerView中显示每个任务的标签和描述。你可以根据自己的需求对适配器和布局进行修改。 希望这个示例能帮助你实现Android 11近期任务的功能!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值