android5.1+获取当前运行的app

2024-08-30 03:32

本文主要是介绍android5.1+获取当前运行的app,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本博客来自于:http://www.itnose.net/detail/6464581.html

android5.1+获取当前运行的app

本人试了,华为5.1是可以的。酷派4.1是不可以的。


/** first app user */
public static final int AID_APP = 10000;
/** offset for uid ranges for each user */
public static final int AID_USER = 100000;
public static String getForegroundApp() {File[] files = new File("/proc").listFiles();int lowestOomScore = Integer.MAX_VALUE;String foregroundProcess = null;for (File file : files) {if (!file.isDirectory()) {continue;}int pid;try {pid = Integer.parseInt(file.getName());} catch (NumberFormatException e) {continue;}try {String cgroup = read(String.format("/proc/%d/cgroup", pid));String[] lines = cgroup.split("\n");String cpuSubsystem;String cpuaccctSubsystem;if (lines.length == 2) {//有的手机里cgroup包含2行或者3行,我们取cpu和cpuacct两行数据cpuSubsystem = lines[0];cpuaccctSubsystem = lines[1];}else if(lines.length==3){cpuSubsystem = lines[0];cpuaccctSubsystem = lines[2];}else {
continue;
}if (!cpuaccctSubsystem.endsWith(Integer.toString(pid))) {// not an application processcontinue;}if (cpuSubsystem.endsWith("bg_non_interactive")) {// background policycontinue;}String cmdline = read(String.format("/proc/%d/cmdline", pid));if (cmdline.contains("com.android.systemui")) {continue;}int uid = Integer.parseInt(cpuaccctSubsystem.split(":")[2].split("/")[1].replace("uid_", ""));if (uid >= 1000 && uid <= 1038) {// system processcontinue;}int appId = uid - AID_APP;int userId = 0;// loop until we get the correct user id.// 100000 is the offset for each user.while (appId > AID_USER) {appId -= AID_USER;userId++;}if (appId < 0) {continue;}// u{user_id}_a{app_id} is used on API 17+ for multiple user account support.// String uidName = String.format("u%d_a%d", userId, appId);File oomScoreAdj = new File(String.format("/proc/%d/oom_score_adj", pid));if (oomScoreAdj.canRead()) {int oomAdj = Integer.parseInt(read(oomScoreAdj.getAbsolutePath()));if (oomAdj != 0) {continue;}}int oomscore = Integer.parseInt(read(String.format("/proc/%d/oom_score", pid)));if (oomscore < lowestOomScore) {lowestOomScore = oomscore;foregroundProcess = cmdline;}} catch (IOException e) {e.printStackTrace();}}return foregroundProcess;
}
private static String read(String path) throws IOException {StringBuilder output = new StringBuilder();BufferedReader reader = new BufferedReader(new FileReader(path));output.append(reader.readLine());for (String line = reader.readLine(); line != null; line = reader.readLine()) {output.append('\n').append(line);}reader.close();return output.toString().trim();//不调用trim(),包名后面会带有乱码
}

这篇关于android5.1+获取当前运行的app的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1119671

相关文章

SpringBoot整合mybatisPlus实现批量插入并获取ID详解

《SpringBoot整合mybatisPlus实现批量插入并获取ID详解》这篇文章主要为大家详细介绍了SpringBoot如何整合mybatisPlus实现批量插入并获取ID,文中的示例代码讲解详细... 目录【1】saveBATch(一万条数据总耗时:2478ms)【2】集合方式foreach(一万条数

python获取网页表格的多种方法汇总

《python获取网页表格的多种方法汇总》我们在网页上看到很多的表格,如果要获取里面的数据或者转化成其他格式,就需要将表格获取下来并进行整理,在Python中,获取网页表格的方法有多种,下面就跟随小编... 目录1. 使用Pandas的read_html2. 使用BeautifulSoup和pandas3.

SpringBoot UserAgentUtils获取用户浏览器的用法

《SpringBootUserAgentUtils获取用户浏览器的用法》UserAgentUtils是于处理用户代理(User-Agent)字符串的工具类,一般用于解析和处理浏览器、操作系统以及设备... 目录介绍效果图依赖封装客户端工具封装IP工具实体类获取设备信息入库介绍UserAgentUtils

Python如何精准判断某个进程是否在运行

《Python如何精准判断某个进程是否在运行》这篇文章主要为大家详细介绍了Python如何精准判断某个进程是否在运行,本文为大家整理了3种方法并进行了对比,有需要的小伙伴可以跟随小编一起学习一下... 目录一、为什么需要判断进程是否存在二、方法1:用psutil库(推荐)三、方法2:用os.system调用

C# foreach 循环中获取索引的实现方式

《C#foreach循环中获取索引的实现方式》:本文主要介绍C#foreach循环中获取索引的实现方式,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、手动维护索引变量二、LINQ Select + 元组解构三、扩展方法封装索引四、使用 for 循环替代

Linux下如何使用C++获取硬件信息

《Linux下如何使用C++获取硬件信息》这篇文章主要为大家详细介绍了如何使用C++实现获取CPU,主板,磁盘,BIOS信息等硬件信息,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录方法获取CPU信息:读取"/proc/cpuinfo"文件获取磁盘信息:读取"/proc/diskstats"文

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

Python运行中频繁出现Restart提示的解决办法

《Python运行中频繁出现Restart提示的解决办法》在编程的世界里,遇到各种奇怪的问题是家常便饭,但是,当你的Python程序在运行过程中频繁出现“Restart”提示时,这可能不仅仅是令人头疼... 目录问题描述代码示例无限循环递归调用内存泄漏解决方案1. 检查代码逻辑无限循环递归调用内存泄漏2.

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

SpringMVC获取请求参数的方法

《SpringMVC获取请求参数的方法》:本文主要介绍SpringMVC获取请求参数的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下... 目录1、通过ServletAPI获取2、通过控制器方法的形参获取请求参数3、@RequestParam4、@