我之前开发了用于应用程序锁的 android 应用程序,该应用程序运行在获取当前运行进程的包名称的想法上,用于低于棒棒糖版本和高于棒棒糖版本。但是现在在 Marshmallow 中,由于通知,存在获取正确包名称的问题。是的,当通知到来时,当前运行进程的包名将更改为通知来自的应用程序包名。简而言之,当我在 Messenger 上时,突然 whatsapp 通知来了,顶级运行进程的包名称将是 com.whatsapp。我的应用程序锁将打开。我正在使用这些方法来获取当前正在运行的应用程序的包名称:
// 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;
}
有什么方法可以让我只在用户界面上获取正在运行的应用程序的包名称,或者从通知中过滤它并将其删除。此问题仅发生在 Marshmallow 中。