Linux 如何找出android应用程序的运行/启动时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3677229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:28:01  来源:igfitidea点击:

How to find out the running/start time of android application?

androidprocess

提问by bhups

Is there anyway to find out the start time of an application?ActivityManagerprovides pids etc for each application process but doesn't tell for how long process is running.

有没有办法找出应用程序的开始时间?ActivityManager为每个应用程序进程提供 pids 等,但不说明进程运行了多长时间。

回答by Cpt.Ohlund

I don't know wether there's an API for this. But one approach would be to use something like:

我不知道是否有用于此的 API。但一种方法是使用类似的东西:

String pid = "yourpid";    
BufferedReader reader = new BufferedReader ( new InputStreamReader ( new FileInputStream ( "ls -ld /proc/"+pid)) , 1000 );

To get the start time of your application and the just subtract that with the currenttime.

获取应用程序的开始时间,然后用当前时间减去它。

Might not be the best way, but that's what came to mind. (And I haven't tried it..)

可能不是最好的方法,但这就是我想到的。(而且我没试过..)

Gl !

啊!

回答by wonder.mice

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