Running process start time

前端 未结 4 2030
失恋的感觉
失恋的感觉 2021-01-14 02:55

I am using below code to get all currently running process\'s on device. How can I get running process start time?

    activityMan = (ActivityManager)getSyst         


        
4条回答
  •  说谎
    说谎 (楼主)
    2021-01-14 03:46

    This will return process start time (since system boot):

    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);
        }
    }
    

    To get process running time:

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

提交回复
热议问题