Android 7.0 7.1 8.0 应用获取各个应用/proc/内存信息的问题

问题由来是:第三方软件(桌面管家)需要获取其他各个app的内存信息,实现一键加速的功能。

原app在获取内存信息的时候,使用了jaredrummler的github开源项目,本质上就是单个应用去访问android的proc底层目录,然后获取数据,再进行加速等操作。

每个应用产生,都会在android底层proc下面产生一个路径,路径的名字以这个应用的pid命名,这个路径下面会有很多关于应用的信息,包括oom_adj等。

获取应用列表的方法是:

/**
* @return a list of all running app processes on the device.
*/
public static List<AndroidAppProcess> getRunningAppProcesses() {
List<AndroidAppProcess> processes = new ArrayList<>();
File[] files = new File("/proc").listFiles();
for (File file : files) {
if (file.isDirectory()) {
int pid;
try {
pid = Integer.parseInt(file.getName());
if(file.getName().matches("[0-9]+")) {
Log.v("dengtl - number path "," getName = " + file.getName()
+ " getAbsolutePath = " +file.getAbsolutePath()
+ " canRead = " + file.canRead());
}
} catch (NumberFormatException e) {
continue;
}
try {
processes.add(new AndroidAppProcess(pid));
} catch (AndroidAppProcess.NotAndroidAppProcessException ignored) {
} catch (IOException e) {
// If you are running this from a third-party app, then system apps will not be
// readable on Android 5.0+ if SELinux is enforcing. You will need root access or an
// elevated SELinux context to read all files under /proc.
// See: https://su.chainfire.eu/#selinux
}
}
}
return processes;
}

由于在7.1系统,应用不允许访问pro目录下的其他应用的pid的路径,于是会报错。

查阅了大量的文章文献,最终表示,在7.1版本后,就不允许访问了,如下:




查看文章链接:


jaredrummler的github开源项目:
发布了4 篇原创文章 · 获赞 4 · 访问量 7851

猜你喜欢

转载自blog.csdn.net/dengtonglong/article/details/79557316