0

I'm designing an application for removing recent tasks. and i use this code for remove recent tasks,

public boolean removeTask(int taskId, int flags) {
    try {
        return (Boolean) mRemoveTask.invoke(mActivityManager, Integer.valueOf(taskId), Integer.valueOf(flags) );
    } catch (Exception ex) {
        Log.i("MyActivityManager", "Task removal failed", ex);
    }
    return false;
}

public void clearRecentTasks() {
    List<ActivityManager.RecentTaskInfo> recents = mActivityManager.getRecentTasks(1000, ActivityManager.RECENT_IGNORE_UNAVAILABLE);
    // Start from 1, since we don't want to kill ourselves!
    for( int i=1; i < recents.size(); i++ ) {
        removeTask( recents.get(i).persistentId, 0);
    }
}

1 Answers1

0

Chaeck this one:

private void killRunningApps(final Context context) {
    this.timer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            List<String> packages = InstalledApps.getRunning(context);
            assert packages != null;
            for (String packageName : packages) {
                AppInfo info = InstalledApps.getAppInfo(packageName, context);
                assert info != null;
                if ((info.getFlag() & ApplicationInfo.FLAG_SYSTEM) == 1) continue;
                manager.killBackgroundProcesses(packageName);
            }
        }
    }, 0, this.updateInterval);
}

At first, you have to declare manager:

    private ActivityManager manager;

in onCreate():

this.manager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
Akbar
  • 430
  • 4
  • 18
  • 2
    thank you.. but this code is for kill background process... i need code for close the recent apps. –  Apr 20 '16 at 13:55
  • This will only close or kill the background and foreground process but it won't remove it from the recent list, to remove it from the recent list use ActivityTaskManager removeTask to remove it from the recent list. – Budhdi Sharma Nov 21 '22 at 07:01