Skip to content

Instantly share code, notes, and snippets.

💭
available

zeeshan mehdi zeeshan-mehdi

💭
available
Block or report user

Report or block zeeshan-mehdi

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@zeeshan-mehdi
zeeshan-mehdi / lollipop.java
Created Jul 13, 2018 — forked from plateaukao/lollipop.java
How to detect foreground process name in Android with Lollipop
View lollipop.java
@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);
You can’t perform that action at this time.