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

一.OS Android5.1

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

	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();
        }
    }

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

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

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();
			}
		}
	}
}

 清单文件 自定义广播

			
	<receiver
            android:name=".recent.CloseTaskReceiver">
            <intent-filter>
                <action android:name="com.xinhua.closeallapp" />
		<action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
		

  

  

  

 

上一篇:done FINRA 史上最简单又最蜜汁自信的abi面试官


下一篇:四. 控制器与运算器(CPU)