1

I need to know when a specific app is in the background (not foreground).

I was able to get package name of all the applications running in the background with this code:

ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(ACTIVITY_SERVICE);
// Process running
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    UsageStatsManager mUsageStatsManager = (UsageStatsManager)getSystemService(USAGE_STATS_SERVICE);
    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);
            Log.d("RunningAppProcessInfo","Package name : "+usageStats.getPackageName());
        }

    }
} 

But now how do I check if the specific app is on the list or not?

Thanks

Ploni
  • 11
  • 1
  • 2

1 Answers1

0

If you want to check Specific Package than check below code I have added code in below method.

ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(ACTIVITY_SERVICE);
// Process running
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    UsageStatsManager mUsageStatsManager = (UsageStatsManager)getSystemService(USAGE_STATS_SERVICE);
    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);
            Log.d("RunningAppProcessInfo","Package name : "+usageStats.getPackageName());

        if(usageStats.getPackageName().equals("package name")) {
          Log.d("Package ","Package name : Success");
         }

        }

    }
}
Sumit Kumar
  • 263
  • 1
  • 6
  • Thanks it works. But it could be that the code I provided is incorrect, because the list also includes apps that do not run in the background, and even an app that I deleted! Do I need to change anything in the code? – Ploni Apr 11 '22 at 17:04
  • I was able to do this with [queryEvents](https://stackoverflow.com/q/38971472/18769941). Thanks. – Ploni Apr 11 '22 at 21:14
  • boolean isNamedProcessRunning(String processName){ if (processName == null) return false; ActivityManager manager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE); List processes = manager.getRunningAppProcesses(); for (RunningAppProcessInfo process : processes) { if (processName.equals(process.processName)) { return true; } } return false; } – Sumit Kumar Apr 12 '22 at 05:28
  • Check above code if you want to get packages that are running in background – Sumit Kumar Apr 12 '22 at 05:28
  • Thanks. But for Lollipop and above it only works for [current application](https://stackoverflow.com/q/30619349/18769941), and I need to check for another application. – Ploni Apr 12 '22 at 19:45
  • I was able to do this [here](https://stackoverflow.com/q/71835401/18769941) with queryEvents, but only for foreground, without background. – Ploni Apr 12 '22 at 21:11