我之前開發的應用程序鎖的android應用程序正在運行在獲取當前正在運行的進程的包名以下棒棒糖和以上棒棒糖版本的包名稱。但現在在棉花糖有一個問題,因爲通知得到正確的包名。是的,當通知傳出時,當前正在運行的進程的軟件包名稱將更改爲通知來自的應用程序軟件包名稱。總之,當我使用Messenger時,突然發出WhatsApp通知,並且頂級運行進程的包名稱將爲com.whatsapp。我的應用程序鎖將打開。我使用這些方法來獲得當前正在運行的應用程序的軟件包名稱:UsageStatsManager開始從新通知中返回包名而不是當前運行
// 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;
}
有沒有的我如何獲得用戶界面上運行的應用程序或通知過濾器,並刪除它的唯一的包名稱的任何方法。此問題僅從棉花糖發生。
想過閱讀通知的權限? – JacksOnF1re