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

发布于 2024-09-18 06:14:12 字数 85 浏览 5 评论 0原文

有没有办法找出应用程序的启动时间?ActivityManager 为每个应用程序进程提供 pid 等,但不告诉进程运行了多长时间。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

复古式 2024-09-25 06:14:12

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

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

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());
卖梦商人 2024-09-25 06:14:12

不知道有没有API可以实现这个功能。但一种方法是使用类似的方法:

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

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

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

GL!

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

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 !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文