사용자가 앱을 열 때마다 내 서비스에서 식별하는 서비스를 개발하려고합니다. 저는/proc/[pid]/cgroup,/proc/[pid]/cmdline,/proc/[pid]/oom_score,/proc/[pid]/oom_score_adj 파일을 사용하여 실행중인 사용자 응용 프로그램인지 확인합니다. 전경에서. 실제로 작동하지만, 게임을 열려고하면 서비스가 항상 인식하지 못합니다. (서비스는 가장 낮은 값을 갖는 파일 (oom_score) 만 식별합니다.Android, 전경에서 앱을 얻는 방법
예 : "com.google.android.googlequicksearchbox : interactor"에 대한 oom_score가 75이지만 "com.king.candycrushsaga"에 대한 oom_score가 150을 초과하므로 코드에 의해 감지되지 않습니다 (다음과 같이) .
서비스 코드 : 응용 프로그램 실행을 얻을 수
scheduler.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
s=appManager.getAppRunningForeground();
System.out.println(s);
}
},1,2,SECONDS);
기능 : 파일을 읽고
private ReadWriteFile readWriteFile = new ReadWriteFile();
public String getAppRunningForeground(){
int pid;
File[] files = new File("/proc").listFiles();
int lowestOomScore = Integer.MAX_VALUE;
String foregroundProcess = null;
for (File file : files) {
if (!file.isDirectory() || (!file.getName().matches(("\\d+"))))
continue;
pid = Integer.parseInt(file.getName());
try {
String cgroup = readWriteFile.read(String.format("/proc/%d/cgroup", pid));
String[] lines = cgroup.split("\n");
if (lines.length != 2)
continue;
String cpuSubsystem = lines[0];
String cpuaccctSubsystem = lines[1];
if (!cpuaccctSubsystem.endsWith(Integer.toString(pid)) || cpuSubsystem.endsWith("bg_non_interactive"))
continue;
String cmdline = readWriteFile.read(String.format("/proc/%d/cmdline", pid));
if (cmdline.contains("com.android.systemui")||cmdline.contains("com.google.android.googlequicksearchbox:interactor")) {
continue;
}
int uid = Integer.parseInt(cpuaccctSubsystem.split(":")[2].split("/")[1].replace("uid_", ""));
if (uid > 1000 && uid <= 1038)//System process
continue;
File oomScoreAdj = new File(String.format("/proc/%d/oom_score_adj", pid));
if (oomScoreAdj.canRead()) {
int oomAdj = Integer.parseInt(readWriteFile.read(oomScoreAdj.getAbsolutePath()));
if (oomAdj != 0) {
continue;
}
}
int oomscore = Integer.parseInt(readWriteFile.read(String.format("/proc/%d/oom_score", pid)));
if (oomscore < lowestOomScore) {
lowestOomScore = oomscore;
foregroundProcess = cmdline.replaceAll("\\p{Cntrl}", "");
}
} catch (IOException e) {
e.printStackTrace();
}
}
return foregroundProcess;
}
클래스입니다.
public class ReadWriteFile{
File file;
StringBuilder teste3 = new StringBuilder();
FileOutputStream outputStream;
public 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();
}
}
피씨 : getRunningTasks는 권장되지 않으므로 제안하지 마십시오.
당신에게 정말 고마워요 도움이 !!!!!! 정말 잘됐다! –
** 정말 고마워. 이 수업에 대해 오전 1시 30 분에 나를 보내신 Gagan Deep **은 절망적이었습니다. 내가 말한대로 – lRadha