进程是否正在运行?
public static boolean isAlive(java.lang.Process process) {
if (process == null) {
return false;
}
boolean isAlive = false;
try {
process.exitValue();
} catch (Throwable ignored) {
isAlive = true;
}
return isAlive;
}
获取由本JAR进程运行的进程:(从运行时 /proc 目录枚举而来)
public static List<AndroidProcess> getRunningProcesses() {
try {
return AndroidProcesses.getRunningProcesses();
} catch (Throwable ignored) {
return new ArrayList<AndroidProcess>();
}
}
杀死指定PID进程:
public static void signal(int pID, int signal) {
try {
android.os.Process.sendSignal(pID, signal);
} catch (Throwable ignored) {
}
}
public static void killProcess(int pID) {
try {
android.os.Process.killProcess(pID);
} catch (Throwable ignored) {
}
}
public static void kill(int pID) {
signal(pID, Process.SIGINT);
signal(pID, Process.SIGKILL);
killProcess(pID);
}
是否处于被调试得状态?
public static boolean isDebuggable(Context context) {
if (context == null) {
return false;
}
try {
ApplicationInfo info = context.getApplicationInfo();
return (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
} catch (Throwable ignored) {
return false;
}
}
获取当前进程得PID(JAR层)
public static int myPid() {
return android.os.Process.myPid();
}
击杀某个匹配关键字名称得进程
public static void kill(String matchKey) {
if (matchKey == null || matchKey.length() <= 0) {
return;
}
for (AndroidProcess process : AndroidProcesses.getRunningProcesses()) {
String processName = process.name;
if (!processName.contains(matchKey)) {
continue;
}
kill(process.pid);
return;
}
}
获取匹配某个关键字名称得所有PID,一般用于击杀所有特定子进程。
public static @NotNull List<Integer> matchAll(String matchKey) {
List<Integer> pids = new ArrayList<Integer>();
if (matchKey == null || matchKey.length() <= 0) {
return pids;
}
List<AndroidProcess> processes = getRunningProcesses();
for (AndroidProcess process : processes) {
String processName = process.name;
if (!processName.contains(matchKey)) {
continue;
}
pids.add(process.pid);
}
return pids;
}
获取匹配某个关键字名称得所有“AndroidProcess” 包装类实例。
public static @NotNull List<AndroidProcess> getProcesses(String matchKey) {
List<AndroidProcess> processes = new ArrayList<AndroidProcess>();
if (matchKey == null || matchKey.length() <= 0) {
return processes;
}
for (AndroidProcess process : AndroidProcesses.getRunningProcesses()) {
String processName = process.name;
if (!processName.contains(matchKey)) {
continue;
}
processes.add(process);
}
return processes;
}
获取匹配某个关键字名称得单个 “AndroidProcess” 包装类实例。
public static @Nullable AndroidProcess getProcessByName(String matchKey) {
if (matchKey == null || matchKey.length() <= 0) {
return null;
}
for (AndroidProcess process : AndroidProcesses.getRunningProcesses()) {
String processName = process.name;
if (!processName.contains(matchKey)) {
continue;
}
return process;
}
return null;
}
通过进程得PID获取确切正在运行得其进程 “AndroidProcess” 包装类实例。
public static @Nullable AndroidProcess getProcessById(int pID) {
for (AndroidProcess process : AndroidProcesses.getRunningProcesses()) {
String processName = process.name;
if (process.pid != pID) {
continue;
}
return process;
}
return null;
}