I just tested my app and CM, ATM Android Assistant, etc. All of them can not get the running process list but they works fine on pre OS version. So what\'s going on with Androi
Another option is to use UsageStatsManager
.
UsageStatsManager mUsageStatsManager = (UsageStatsManager)getSystemService(Context.USAGE_STATS_SERVICE);
long endTime = System.currentTimeMillis();
long beginTime = endTime - 1000*60;
// We get usage stats for the last minute
List stats = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, beginTime, endTime);
// Sort the stats by the last time used
if(stats != null)
{
SortedMap mySortedMap = new TreeMap();
for (UsageStats usageStats : stats)
{
mySortedMap.put(usageStats.getLastTimeUsed(),usageStats);
}
if(mySortedMap != null && !mySortedMap.isEmpty())
{
topActivity = mySortedMap.get(mySortedMap.lastKey()).getPackageName();
}
}
In order for this to work, you need PACKAGE_USAGE_STATS
permission. You can prompt the user to do this by opening the screen in settings:
Intent usageAccessIntent = new Intent( Settings.ACTION_USAGE_ACCESS_SETTINGS );
usageAccessIntent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
startActivity( usageAccessIntent );