Android Running Task - android

How to get the list of applications which are currently running in android system and how to update it on the layout with their icon image.
And also help me to get all recent used application list and how to close one existing running application.
ActivityManager actvityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();
for (int i = 0; i < procInfos.size(); i++) {
String st = procInfos.get(i).processName + " " + String.valueOf(procInfos.get(i).processName.length());
//How to update it on layout in list format
}

Use custom listview.
create an array for app names and another for images(icons)
Then use custom listview to display it.
Here's a demo of custom listview

Related

Fetch list of running activities in Android (from Lollipop devices onwards)

I am working on a personal project where I need to fetch a list of all running packages in android.
I have been able to fetch a list of running applications in pre-lollipop devices using this code.
ActivityManager m = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE );
List<RunningTaskInfo> runningTaskInfoList = m.getRunningTasks(10);
Iterator<RunningTaskInfo> itr = runningTaskInfoList.iterator();
while(itr.hasNext())
{
RunningTaskInfo runningTaskInfo = (RunningTaskInfo)itr.next();
int id = runningTaskInfo.id;
CharSequence desc= runningTaskInfo.description;
String topActivity = runningTaskInfo.topActivity.getShortClassName();
int numOfActivities = runningTaskInfo.numActivities;
}
But it doesn't work with Lollipop devices. Can someone please assist me with this?
Please read the javadoc for getRunningTasks(). Basically, you can't do this any more because it's seen as a security problem for the end user.

Get the RAM usage of a process by using its pid android [duplicate]

i want to log ram usage of a given application at a given time rate. i wrote the code to get the full memory value used but do not know how to get the memory usage of a given PID.
please help me out
this is the code i used to get the memory
ActivityManager localActivityManager = (ActivityManager)getSystemService("activity");
ActivityManager.MemoryInfo localMemoryInfo = new ActivityManager.MemoryInfo();
localActivityManager.getMemoryInfo(localMemoryInfo);
Log.i("",String.valueOf(localMemoryInfo.availMem));
i think i can get it using public MemoryInfo[] getProcessMemoryInfo (int[] pids), but do not know how to code for it since im a android beginer
ActivityManager localActivityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE); // use Context.ACTIVITY_SERVICE not the literal "activity"
List<ActivityManager.RunningAppProcessInfo> procsInfo = localActivityManager.getRunningAppProcesses();
int[] pids = new int[procsInfo.size()];
for (int i = 0; i < procsInfo.size(); i++) {
ActivityManager.RunningAppProcessInfo info = procsInfo.get(i);
pids[i] = info.pid;
}
Debug.MemoryInfo[] procsMemInfo = localActivityManager.getProcessMemoryInfo(pids);
// now walk the procsMemInfo array
If you schedule a recurring timer to periodically re-query for running pids and query for memory info you can use timestamps to compute the memory usage over time.

android: how to get memory usage (ram) of a given PID programically

i want to log ram usage of a given application at a given time rate. i wrote the code to get the full memory value used but do not know how to get the memory usage of a given PID.
please help me out
this is the code i used to get the memory
ActivityManager localActivityManager = (ActivityManager)getSystemService("activity");
ActivityManager.MemoryInfo localMemoryInfo = new ActivityManager.MemoryInfo();
localActivityManager.getMemoryInfo(localMemoryInfo);
Log.i("",String.valueOf(localMemoryInfo.availMem));
i think i can get it using public MemoryInfo[] getProcessMemoryInfo (int[] pids), but do not know how to code for it since im a android beginer
ActivityManager localActivityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE); // use Context.ACTIVITY_SERVICE not the literal "activity"
List<ActivityManager.RunningAppProcessInfo> procsInfo = localActivityManager.getRunningAppProcesses();
int[] pids = new int[procsInfo.size()];
for (int i = 0; i < procsInfo.size(); i++) {
ActivityManager.RunningAppProcessInfo info = procsInfo.get(i);
pids[i] = info.pid;
}
Debug.MemoryInfo[] procsMemInfo = localActivityManager.getProcessMemoryInfo(pids);
// now walk the procsMemInfo array
If you schedule a recurring timer to periodically re-query for running pids and query for memory info you can use timestamps to compute the memory usage over time.

Android - get actual allocated RAM size

when I open new activities, I need to know my actual memory allocation (app can have huge memory allocation and I need to test real numbers). Is there any API for that?
Thanks
Edit code is not running... there is error on last line:
ActivityManager actvityManager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
ActivityManager.MemoryInfo mInfo = new ActivityManager.MemoryInfo ();
actvityManager.getMemoryInfo( mInfo );
List<RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();
for(int i = 0; i < procInfos.size(); i++)
{
if(procInfos.get(i).processName.equals("cz.process.a")) {
int field[]=new int []{procInfos.get(i).pid};
android.os.Debug.MemoryInfo[] info = actvityManager.getProcessMemoryInfo(field);
Still not solved. There was someone, who was an expert. But he offered me a code and wasn't able to describe it. He suggested code above, but on the last line there is a critical error.
I believe Debug and Debug.MemoryInfo classes can help you.

What i am doing wrong to get the UID

I saw similar types of posts here.But i am not getting this right.To get the UID of running process i wrote
ActivityManager mgr = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> processes = mgr.getRunningAppProcesses();
String text = "All Process:\n";
for (int i = 1; i <= processes.size(); i++)
{
String s;
s = processes.get(i - 1).processName.toString();
text += "Process:" + i + s + ":UID:" + android.os.Process.getUidForName(s) + "\n";
}
But after completion of loop what i am getting in the string text is all UID value as -1.I put GET_TASKS permission in manifest file.Why i am not getting the UID.Please help.I need this UID to kill the process.
To kill the process i used killBackgroundProcess Method of ActivityManager.It needs package name not the UID
Please see this answer by #seanhodges for reference.
Reading the whole thread might be helpful too.

Categories

Resources