Android 学习任务缩略图

  • 运行环境

    1、下面案例在系统签名下可以运行 版本:Android 11
    2、注意:我尝试在没有系统签名下打开
//代码中
File xmlFile = new File("/data/system_ce/0/recent_tasks/33_task.xml");  

会报以下错误

2023-04-10 16:23:38.278 4411-4438/com.example.myapplication W/System.err: java.io.FileNotFoundException: /data/system_ce/0/recent_tasks/33_task.xml: open failed: EACCES (Permission denied)
2023-04-10 16:23:38.279 4411-4438/com.example.myapplication W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:492)
2023-04-10 16:23:38.279 4411-4438/com.example.myapplication W/System.err:     at java.io.FileInputStream.<init>(FileInputStream.java:160)

<!--AndroidManifest.xml  申明存储权限  -->  
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>  
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>

目前还找不到解决方法、猜测是谷歌做了限制
  • 目录保存位置

    1、可以了解一下进程的xml文件位置 /data/system_ce/0/recent_tasks/296_task.xml
    2、任务缩略图片位置 /data/system_ce/0/snapshots
    For example: 296_task.xml
	<?xml version='1.0' encoding='utf-8' standalone='yes' ?>  
<task affinity="10030:us.zoom.videomeetings" 
	asked_compat_mode="false"  
    auto_remove_recents="false" 
    calling_feature_id=""  calling_package="us.zoom.videomeetings"  
    calling_uid="10030"  effective_uid="10030"                           
    last_time_moved="1681107927186"  min_height="-1"  
    min_width="-1"  never_relinquish_identity="true" 
    next_affiliation="-1"  
    persist_task_version="1" prev_affiliation="-1"  

real_activity="us.zoom.videomeetings/com.zipow.videobox.LauncherActivity"  
	
	real_activity_suspended="false" resize_mode="2"  
    root_has_reset="true" supports_picture_in_picture="false" 
    task_affiliation="296"  task_affiliation_color="-13026995" 
    task_description_color="ff39394d" task_description_colorBackground="ffffffff" 

    task_id="296"  
    
    user_id="0" user_setup_complete="true">  
    <intent action="android.intent.action.MAIN"  
		component="us.zoom.videomeetings/com.zipow.videobox.LauncherActivity" 
		flags="10200000">  
    <categories category="android.intent.category.LAUNCHER" />  
    </intent>
</task>
这里着重看一下       
//当前活动
real_activity="us.zoom.videomeetings/com.zipow.videobox.LauncherActivity"  
//表示进程ID   以后通过进程Id 获取任务缩略图      
//data/system_ce/0/snapshots/296_reduced.jpg    文件夹位置 
task_id="296"    
  • 获取正在运行的进程信息

    public void getAppTasks(ObservableEmitter<List<TaskBean>> observableEmitter)  {  
    this.isExist = false;  
    PackageManager packageManager = this.mContext.getPackageManager();  
    ArrayList<TaskBean> arrayList = new ArrayList<>();  
    List<ActivityManager.RecentTaskInfo> recentTasks = 
    this.activityManager.getRecentTasks(18, 2);  
    if (recentTasks == null || recentTasks.size() == 0) {  
        observableEmitter.onNext(arrayList);  
        return;    }  
  
    //找到正在运行的App  
    for (ActivityManager.RecentTaskInfo recentTaskInfo : recentTasks) {  
        ResolveInfo resolveActivity = 
        packageManager.resolveActivity(recentTaskInfo.baseIntent, 0);  
        String str = resolveActivity.activityInfo.packageName;  
        //过滤自己不想展示的Apk  isNotShowApp(this.mContext, str)
        if (!isNotShowApp(this.mContext, str)) {  
            TaskBean taskBean = new TaskBean();  
            taskBean.setBaseIntent(recentTaskInfo.baseIntent);  
            taskBean.setName(resolveActivity.loadLabel(packageManager).toString());  
            //任务缩略图位置
            String appTaskThumbnail = 
      "/data/system_ce/0/snapshots/"+recentTaskInfo.persistentId+"_reduced.jpg";
            taskBean.setThumb(appTaskThumbnail);  
            taskBean.setIcon(getAppIcon(this.mContext, str));  
            taskBean.setPackageName(str);  
            taskBean.setId(recentTaskInfo.id);  
            taskBean.setPersistentId(recentTaskInfo.persistentId);  
            taskBean.setClassName(resolveActivity.activityInfo.name);  
            arrayList.add(taskBean);  
        }  
    }  
    observableEmitter.onNext(arrayList);  
}
public class TaskBean {  
    private Intent baseIntent;  
    private String className;  
    private Drawable icon;  
    private int id;  
    private String name;  
    private String packageName;  
    private int persistentId;   
    private String thumb;  
  
    public int getId() {  
        return this.id;  
    }  
  
    public void setId(int i) {  
        this.id = i;  
    }  
  
    public int getPersistentId() {  
        return this.persistentId;  
    }  
  
    public void setPersistentId(int i) {  
        this.persistentId = i;  
    }  
  
    public String getName() {  
        return this.name;  
    }  
  
    public void setName(String str) {  
        this.name = str;  
    }  
  
    public Drawable getIcon() {  
        return this.icon;  
    }  
  
    public void setIcon(Drawable drawable) {  
        this.icon = drawable;  
    }  
  
    public String getThumb() {  
        return this.thumb;  
    }  
  
    public void setThumb(String str) {  
        this.thumb = str;  
    }  
  
    public String getPackageName() {  
        return this.packageName;  
    }  
  
    public void setPackageName(String str) {  
        this.packageName = str;  
    }  
  
    public String getClassName() {  
        return this.className;  
    }  
  
    public void setClassName(String str) {  
        this.className = str;  
    }  
  
    public Intent getBaseIntent() {  
        return this.baseIntent;  
    }  
  
    public void setBaseIntent(Intent intent) {  
        this.baseIntent = intent;  
    }  
}
  • 展示(建议通过RecycleView 展示)

  //itemView  绑定数据
  public void bindTask(TaskBean taskBean) {    
    this.mIvAppIcon.setImageDrawable(taskBean.getIcon());  
    this.mTvAppName.setText(taskBean.getName());  
    String thumb = taskBean.getThumb();  
    if (TextUtils.isEmpty(thumb)) {  
        return;  
    }  
    File file = new File(thumb);  
    if (file.exists() && file.isFile()) {  
    //通过这里获取任务缩略图
    this.mIvTaskSnapShot.setImageBitmap(BitmapFactory.decodeFile(thumb));  
    }  
}
//itemView xml文件
<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout 
	xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:app="http://schemas.android.com/apk/res-auto"  
    android:id="@+id/rl_container"  
    android:layout_width="680px"  
    android:layout_height="wrap_content"  
    android:layout_margin="24px"  
    android:clipChildren="false"  
    android:clipToPadding="false"  
    android:pointerIcon="hand">  
  
    <LinearLayout        
	    android:id="@+id/ll_header"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:gravity="center_vertical"  
        android:paddingVertical="20px">  
  
        <ImageView            
	        android:id="@+id/iv_app_icon"  
            android:layout_width="40px"  
            android:layout_height="40px"  
            android:scaleType="fitCenter" />  
  
        <TextView            
	        android:id="@+id/tv_app_name"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_marginStart="@dimen/x10"  
            android:textColor="@color/white"  
            android:textSize="40px" />  
    </LinearLayout>  
    
    <androidx.cardview.widget.CardView        
	    android:layout_width="@dimen/x340"  
        android:layout_height="@dimen/y191"  
        android:layout_below="@+id/ll_header"  
        app:cardCornerRadius="24px"  
        app:cardElevation="0dp">  
  
        <ImageView           
	         android:id="@+id/iv_task_snap_shot"  
            android:layout_width="match_parent"  
            android:layout_height="match_parent"  
            android:scaleType="centerCrop" />  
    </androidx.cardview.widget.CardView>
    
</RelativeLayout>
For example

  • 恢复指定活动页面

   public void onTaskClick(TaskBean taskBean) {  
    //通过进程ID 判断是否存活  
    if (taskBean.getId() >= 0) {  
	    ((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE)).moveTaskToFront(taskBean.getId(), ActivityManager.MOVE_TASK_WITH_HOME);  
    } else {  
        startActivity(taskBean.getBaseIntent());  
    }  
}
posted @ 2023-04-11 09:53  炸憨啪  阅读(144)  评论(0编辑  收藏  举报