-
-
Save TwizzyIndy/bd78a2ffef2561fde313 to your computer and use it in GitHub Desktop.
How to detect foreground process name in Android with Lollipop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@TargetApi(Build.VERSION_CODES.LOLLIPOP) | |
public static String getForegroundProcess(Context context) { | |
String topPackageName = null; | |
UsageStatsManager usage = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE); | |
long time = System.currentTimeMillis(); | |
List<UsageStats> stats = usage.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000*1000, time); | |
if (stats != null) { | |
SortedMap<Long, UsageStats> runningTask = new TreeMap<Long,UsageStats>(); | |
for (UsageStats usageStats : stats) { | |
runningTask.put(usageStats.getLastTimeUsed(), usageStats); | |
} | |
if (runningTask.isEmpty()) { | |
return null; | |
} | |
topPackageName = runningTask.get(runningTask.lastKey()).getPackageName(); | |
} | |
if(topPackageName==null) { | |
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS); | |
context.startActivity(intent); | |
} | |
return topPackageName; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment