I was previously developed android application for app lock which is running on Idea of getting current running process's package name for both below lollipop and above lollipop versions. But Now In Marshmallow there is a problem of getting correct package name because of notifications. Yes, when Notification came, package name of current running process is changed to the apps package name from which notification came. In short when I'm on messenger and suddenly whatsapp notification came and top running process's package name will be com.whatsapp. and my app lock will open. I'm using these methods to get current running Application's package name:
// API below 21
@SuppressWarnings("deprecation")
public static String getProcessOld(Context c) throws Exception {
String topPackageName = null;
ActivityManager activity = (ActivityManager) c.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> runningTask = activity.getRunningTasks(1);
if (runningTask != null) {
RunningTaskInfo taskTop = runningTask.get(0);
ComponentName componentTop = taskTop.topActivity;
topPackageName = componentTop.getPackageName();
}
return topPackageName;
}
// API 21 and above
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static String getProcessNew(Context c) throws Exception {
String st = null;
try {
// st = getProcess();
UsageStatsManager mUsageStatsManager = (UsageStatsManager) c.getSystemService("usagestats");
long endTime = System.currentTimeMillis();
long beginTime = endTime - 1000 * 10;
// We get usage stats for the last minute
List<UsageStats> stats = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_BEST, beginTime,
endTime);
// 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()) {
st = mySortedMap.get(mySortedMap.lastKey()).getPackageName();
}
}
} catch (Exception e) {
Log.d("main", "Wxxxxexx " + e);
}
return st;
}
Is there any method of how can i get only package name of running application on User interface or filter it from notification and remove it. This problem occurs only from Marshmallow.