运行过程开始时间

时间:2011-04-05 12:53:21

标签: android

我使用下面的代码来获取设备上当前正在运行的所有进程。如何获得正在运行的进程启动时间?

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

4 个答案:

答案 0 :(得分:3)

这将返回进程开始时间(自系统引导以来):

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

答案 1 :(得分:0)

据我所知,您只能从服务中接收此信息。请查看documentation of ActivityManager.RunningServiceInfoactiveSince attribute

答案 2 :(得分:0)

补充上述答案..

private static long getProcessStartTime(final int pid) throws Exception {
    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]);
        int tckName;
        try {
            tckName = Class.forName("android.system.OsConstants").getField("_SC_CLK_TCK").getInt(null);
        } catch (ClassNotFoundException e) {
            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 (Exception e) {
        throw new Exception(e);
    } 
}

答案 3 :(得分:0)

使用kotlin和API 21,上面的代码变为

@Throws(IOException::class)
private fun getStartTime( pid:Int ) : Long { 
    val reader = BufferedReader(FileReader ("/proc/$pid/stat"));
    val stats = try {
        reader.readLine();
    } finally {
        reader.close();
    }
    val fieldStartTime = 20;
    val msInSec = 1000;
    try {
        val fields = stats.substring (stats.lastIndexOf(") ")).split(" ");
        val t = fields[fieldStartTime].toLong();
        val tck = Os.sysconf(OsConstants._SC_CLK_TCK);
        return (t * msInSec) / tck;
    } catch (e: NumberFormatException) {
        throw IOException (e);
    } catch (e: IndexOutOfBoundsException ) {
        throw IOException (e);
    }
}