0

I need to find a way to remove (or clear) an app from the recent app menu. I don't want to remove my app from the list, I want to remove a recent app according to my choice. What I got is the name of the app (packageName). I can't find any way to do that, so I will be greatful if I will find an answer. Android Platform, Java API +19 or +21. Best Regards

uriburg
  • 19
  • 5
  • Assuming that by "recent app menu", you mean the overview screen/recent tasks screen, I do not think that you have a way of removing tasks associated with other apps. – CommonsWare Mar 24 '17 at 16:03
  • @CommonsWare Yes, I mean the recent tasks screen.... – uriburg Mar 24 '17 at 16:06

1 Answers1

0

You can get the list of the recent apps first to check if the packagename exists in the recents list. It can be done in this manner -

String topPackageName ;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
    UsageStatsManager mUsageStatsManager = (UsageStatsManager)getSystemService("usagestats");                       
    long time = System.currentTimeMillis(); 
    // We get usage stats for the last 10 seconds
    List<UsageStats> stats = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000*10, time);                                    
    // Sort the stats by the last time used
    if(stats != null) {
        SortedMap<Long,UsageStats> mySortedMap = new TreeMap<Long,UsageStats>();
        for (UsageStats usageStats : stats) {
            mySortedMap.put(usageStats.getLastTimeUsed(),usageStats);
        }                    
        if(mySortedMap != null && !mySortedMap.isEmpty()) {
            topPackageName =  mySortedMap.get(mySortedMap.lastKey()).getPackageName();                                   
        }                                       
    }
}  

Snippet Source How to get recent tasks

Once that is done, you can terminate the app using killBackgroundProcesses on a ActivityManager object like this -

ActivityManager actMan = (ActivityManager) getSystemService(Activity.ACTIVITY_SERVICE);
actMan.killBackgroundProcesses(killAppPackageName);

Note that you need the KILL_BACKGROUND_PROCESSES permission. So Add this in your manifest

<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
Community
  • 1
  • 1
Sanved
  • 921
  • 13
  • 19