In my app , I am showing the memory usage of each app. I tried the solutions provided in SOF as,
Getting from MemInfo shell command
Process process = Runtime.getRuntime().exec("dumpsys meminfo " + info.packageName);
Permission denied to run this command.
Tried with TOP command
Process process = Runtime.getRuntime().exec("top -n 1 -d 1");
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
for(int i=0;i<5;i++)
in.readLine();
do {
try {
String trial[] = in.readLine().split(" ");
int j=0;
String array[]=new String[trial.length];
for(String a:trial)
{
if(!a.isEmpty()) {
array[j] = a;
j++;
}
}
String package_name=array[9];
if(package_name!=null)
{
if(package_name.contains(":"))
package_name=package_name.substring(0,package_name.indexOf(":"));
}
publishProgress(package_name);
Debug.MemoryInfo[] procsMemInfo = manager.getProcessMemoryInfo(new int[] {Integer.parseInt(array[0])});
long memory=0;
for (Debug.MemoryInfo info:procsMemInfo)
{
memory+=info.getTotalPss();
}
processDescriptions.add(new ProcessDescription(array[0], Integer.parseInt(array[1]),
array[2], MemorySizeConverter.MBorKB_Converter((double) memory, 1), array[8], package_name));
}
catch (Exception e)
{
Log.d("skipped",e.toString());
}
} while (in.readLine() != null );
It worked and I can get running processes memory info. Not for all installed apps.
As this statement, My app should be signed with the platform key, or installed in the system partition to access it. But how third party apps available in playstore are accessing it . Can anyone help me.