This is my first StackOverflow-Post so tell me if i do something wrong :) I'm currently trying to create an "AppLock-Like" App, so I somehow need my App to know which other App has been opened or what Apps are currently running.
I've been researching all day now, trying to figure out, how to get a List (Package Names) of all currently running Apps on Android. The only way to do so seems to be the depricated getRunningAppProcesses() from the ActivityManager Class. But since its depricated, it only returns my own App, even when running as a Background Service, while other Apps are beeing opened. And the new UsageStatsManager seems to only give longterm information.
Is there even still a way to get access to that information? There are plenty of Apps in the PlayStore that somehow are able to identify when and what App is opened. Any help is welcomed, thanks already :)
I've tried so much Code, this is my last try:
public static boolean isRunningAppBanned(Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo = am.getRunningAppProcesses();
//Print all running Apps
for (int i = 0; i < runningAppProcessInfo.size(); i++) {
Log.println(Log.INFO, "Running Apps", runningAppProcessInfo.get(i).processName);
}
//Check if a running App is Banned
for (int i = 0; i < runningAppProcessInfo.size(); i++) {
if(Database.isBanned(runningAppProcessInfo.get(i).processName)) {
Log.println(Log.ERROR, "Banned Apps", runningAppProcessInfo.get(i).processName + "IS BANNED");
return true;
}
}
return false;
}