一.OS Android5.1
1.1.应用是否具有android.intent.category.LAUNCHER属性有关,在主Activity有LAUNCHER的前提下,android:excludeFromRecents=“true”,才能达到在最近任务列表中隐藏该应用的目的
1.2.frameworksasepackagesSystemUIsrccomandroidsystemuistatusbaroswinRecentAppManager.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(); } }
1.3. 发送广播 清理后台所有历史App任务
frameworksasepackagesSystemUIsrccomandroidsystemui ecentCloseTaskReceiver.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(); } } } }
1.4.清单文件 自定义广播
<receiver android:name=".recent.CloseTaskReceiver"> <intent-filter> <action android:name="com.xinhua.closeallapp" /> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver>