RK 清理后台所有历史App任务

一.OS Android5.1

1.1.应用是否具有android.intent.category.LAUNCHER属性有关,在主Activity有LAUNCHER的前提下,android:excludeFromRecents=“true”,才能达到在最近任务列表中隐藏该应用的目的

1.2.frameworks\base\packages\SystemUI\src\com\android\systemui\statusbar\oswin\RecentAppManager.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public void removeRecentApp(String packName){
    ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
    PackageManager pm = mContext.getApplicationContext().getPackageManager();
    List<ActivityManager.RecentTaskInfo> appTask = am.getRecentTasks(50,ActivityManager.RECENT_WITH_EXCLUDED|ActivityManager.RECENT_IGNORE_UNAVAILABLE);
    if(!appTask.isEmpty()){
        try {
            for(ActivityManager.RecentTaskInfo ra : appTask){
                Intent intent = new Intent(ra.baseIntent);
                if((isCurrentHomeActivity(intent.getComponent().getPackageName(), null))){
                    continue;
                }
 
                if(intent.getComponent().getPackageName().equals(packName)){
                    int persistentId = ra.persistentId; // pid
                    Log.d(TAG, "removeRecentApp --> packName:" + packName);
                    am.removeTask(persistentId/*, ActivityManager.REMOVE_TASK_KILL_PROCESS*/);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
     }
}
 
 
public void clearRunningTasks(){
    ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RecentTaskInfo> run = am.getRecentTasks(512, ActivityManager.RECENT_IGNORE_UNAVAILABLE);
    PackageManager pm =mContext.getPackageManager();
    try {
        for(ActivityManager.RecentTaskInfo ra : run){
            Intent intent = new Intent(ra.baseIntent);
            if((isCurrentHomeActivity(intent.getComponent().getPackageName(), null))
                    ||(intent.getComponent().getPackageName().equals("com.android.launcher"))
                    || (intent.getComponent().getPackageName().equals("xxxx.xxxx.xxx"))){
             continue;
            }
 
 
            int persistentId = ra.persistentId;
            am.removeTask(persistentId/*, ActivityManager.REMOVE_TASK_KILL_PROCESS*/);
         Toast.makeText(mContext, "Clear Recent APP", 500).show();
        }
    } catch (Exception e) {
       e.printStackTrace();
    }
}

1.3. 发送广播 清理后台所有历史App任务

frameworks\base\packages\SystemUI\src\com\android\systemui\recent\CloseTaskReceiver.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.android.systemui.recent;
 
import com.android.systemui.recent.RecentsPanelView;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.app.ActivityManager;
import android.content.pm.PackageManager;
 
import java.util.List;
 
public class CloseTaskReceiver  extends BroadcastReceiver {
 
    private  Context mContext;
    private  String packName ="bd.nj.onetoonecall";
    static final String BOOT_COMPLETED = "android.intent.action.BOOT_COMPLETED";
 
    @Override
    public void onReceive(Context context, Intent intent) {
        mContext = context;
        String action = intent.getAction();
        if (BOOT_COMPLETED.equals(action)){
            Log.d("gatsby","BOOT_COMPLETED removeRecentApp");
            removeRecentApp();
        }
        if (("com.xinhua.closeallapp").equals(action)) {
             Log.d("gatsby","com.xinhua.closeallapp");
             removeRecentApp();
        }
    }
 
    public void removeRecentApp(){
        ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
        PackageManager pm = mContext.getApplicationContext().getPackageManager();
        List<ActivityManager.RecentTaskInfo> appTask = am.getRecentTasks(50,ActivityManager.RECENT_WITH_EXCLUDED|ActivityManager.RECENT_IGNORE_UNAVAILABLE);
        if(!appTask.isEmpty()){
            try {
                for(ActivityManager.RecentTaskInfo ra : appTask){
                    Intent intent = new Intent(ra.baseIntent);
                    if(!intent.getComponent().getPackageName().equals(packName)){
                        int persistentId = ra.persistentId; // pid
                        am.removeTask(persistentId/*, ActivityManager.REMOVE_TASK_KILL_PROCESS*/);
                    }else{
                        Log.d("gatsby", "removeRecentApp --> packName:" + packName);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

 1.4.清单文件 自定义广播

1
2
3
4
5
6
7
8
9
         
<receiver
        android:name=".recent.CloseTaskReceiver">
        <intent-filter>
            <action android:name="com.xinhua.closeallapp" />
    <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
    

  

 

posted @   CrushGirl  阅读(384)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示