问题
Is there a way to read out all processes, not only the running processes.
If I understood Android correctly, only one process is running at a time, and every other process is frozen (background processes ignored)
回答1:
You can get all of androids currently running app processes by using the following code snippet:
ActivityManager activityManager = (ActivityManager)BaseContext.GetSystemService(Context.ActivityService);
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
activityManager.GetMemoryInfo(memoryInfo);
List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = activityManager.RunningAppProcesses as List<ActivityManager.RunningAppProcessInfo>;
You can then loop through the list called runningAppProcesses
and interrogate the process information class for other bits of information if required, these can be found listed here.
Here's a couple of examples:
ProcessName - The name of the process that this object is associated with.
Pid - The pid of this process; 0 if none.
Uid - The user id of this process.
EDIT:
As per the ops comment below, that they need 'frozen' (suspended) processes, here's some additional information:
It used to be that you could grab a list of running tasks, not quite the same as processes but are defined by android as:
Return a list of the tasks that are currently running, with the most recent being first and older ones after in order. Note that "running" does not mean any of the task's code is currently loaded or activity -- the task may have been frozen by the system, so that it can be restarted in its previous state when next brought to the foreground.
List<ActivityManager.RunningTaskInfo>
However according to android documentation this functionality was depreciated in API level 21.
As of Build.VERSION_CODES.LOLLIPOP, this method is no longer available to third party applications: the introduction of document-centric recents means it can leak person information to the caller. For backwards compatibility, it will still return a small subset of its data: at least the caller's own tasks, and possibly some other tasks such as home that are known to not be sensitive.
I don't think you are able to do what you want for the reasons quoted by Android above, for more information you can head to this link.
来源:https://stackoverflow.com/questions/56621465/xamarin-android-get-all-processes-located-in-memory