gpt4 book ai didi

android - 运行进程开始时间

转载 作者:太空狗 更新时间:2023-10-29 12:56:52 26 4
gpt4 key购买 nike

我正在使用以下代码获取设备上所有当前正在运行的进程。如何获取运行进程的开始时间?

    activityMan = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
process = activityMan.getRunningAppProcesses();
for (Iterator iterator = process.iterator(); iterator.hasNext();) {
RunningAppProcessInfo runningAppProcessInfo = (RunningAppProcessInfo) iterator
.next();
pSname= runningAppProcessInfo.processName;
System.out.println(pSname);
}

最佳答案

这将返回进程启动时间(自系统启动后):

private static long getStartTime(final int pid) throws IOException {
final String path = "/proc/" + pid + "/stat";
final BufferedReader reader = new BufferedReader(new FileReader(path));
final String stat;
try {
stat = reader.readLine();
} finally {
reader.close();
}
final String field2End = ") ";
final String fieldSep = " ";
final int fieldStartTime = 20;
final int msInSec = 1000;
try {
final String[] fields = stat.substring(stat.lastIndexOf(field2End)).split(fieldSep);
final long t = Long.parseLong(fields[fieldStartTime]);
final int tckName = Class.forName("libcore.io.OsConstants").getField("_SC_CLK_TCK").getInt(null);
final Object os = Class.forName("libcore.io.Libcore").getField("os").get(null);
final long tck = (Long)os.getClass().getMethod("sysconf", Integer.TYPE).invoke(os, tckName);
return t * msInSec / tck;
} catch (final NumberFormatException e) {
throw new IOException(e);
} catch (final IndexOutOfBoundsException e) {
throw new IOException(e);
} catch (ReflectiveOperationException e) {
throw new IOException(e);
}
}

获取进程运行时间:

final long dt = SystemClock.elapsedRealtime() - getStartTime(Process.myPid());

关于android - 运行进程开始时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5552125/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com