Android 11 近期任务demo
时间: 2023-08-18 22:03:53 浏览: 61
以下是一个简单的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近期任务的功能!如果还有其他问题,请随时提问。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)