22

whenever I try to query the Usage Stats of the UsageStatsManager I can correctly get the last running app. However, if I pull down the status bar and there is a new notification, the last used app (based on UsageStats) will change to that of the notification. As a result, I get false alarms that the foreground application has changed.

I can't seem to find a way to filter those specific draws. Any ideas?

Right now, I query for the foreground app with the following code. The problem exists only in Marshmallow (5.X works correctly).

UsageStatsManager mUsageStatsManager = (UsageStatsManager) rotationManager.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 * 2, 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()) {
    foregroundApp = mySortedMap.get(mySortedMap.lastKey()).getPackageName();
} 
}
Md. Sabbir Ahmed
  • 850
  • 8
  • 22
fcdimitr
  • 528
  • 3
  • 16

3 Answers3

13

You can query the usageEvent to check if the last active app's event is UsageEvents.Event.MOVE_TO_FOREGROUND.

After you get the foregroundApp, you can refer to the following code:

 UsageEvents usageEvents = mUsageStatsManager.queryEvents(time - 100 * 1000, time);
 UsageEvents.Event event = new UsageEvents.Event();
 // get last event
 while (usageEvent.hasNextEvent()) {
     usageEvent.getNextEvent(event);
 }
 if (foregroundApp.equals(event.getPackageName()) && event.getEventType() == UsageEvents.Event.MOVE_TO_FOREGROUND) {
     return foregroundApp ;
 }
Lingkun Kong
  • 346
  • 2
  • 6
  • 1
    Thank you very much! This solves the problem for now... until Google decides to change it again :)... – fcdimitr Nov 29 '15 at 17:04
  • A very big thank you for this answer. I am looking for this from many days. again thank you very much its working very well. :) – WonderSoftwares Jul 01 '16 at 12:20
  • 1
    Solution is working but there is little problem. Foreground app package is detecting after 3 seconds delay in marshmallow.By debugging, queryUsageStats returns quick package info and after that queryEvents taking 3 seconds to get correct package in foreground. – WonderSoftwares Jul 28 '16 at 09:41
3

In android 6.0 os, getUsageStats function giving list size zero. For confirmation, you can check any lock app from play store. Those app are not working in android 6.0.

d.k.
  • 415
  • 4
  • 16
  • I check it is not working in some devices like micro max. So can you describe what is mean of get false alarm? – d.k. Oct 26 '15 at 13:39
  • getUsageStats actually returns the last used applications, but also returns, on the top of the list, the last app that was drawn on the status bar, even without opening it. That's why I say false alarm, because it says that the front app has changed, while it has remained the same. – fcdimitr Oct 26 '15 at 16:18
  • have you checked last used time of applications which are on top. Because i see in marshmallow, This api always give some applications list on top with last time used zero. So ignore those application with last used time zero and check with non zero time values. From non zero last time used list, you'll get your application. Let me know, It solved your issue? – d.k. Oct 27 '15 at 04:58
  • Thank you for your suggestions, however the application that is drawn on the status bar (a notification) has last time used non-zero, so I can't tell it apart from the ongoing foreground app. – fcdimitr Oct 27 '15 at 09:41
  • peraphs is possible compare notification post time with time in usagestats (last time used)? – Davide Oct 30 '15 at 22:23
2

Take a look at https://github.com/ricvalerio/foregroundappchecker, it might be what you need. Provides sample code, and takes away the pain of having to implement cross version foreground detector.

rvalerio
  • 489
  • 5
  • 5