如何找出运行/启动Android应用程序的时间呢? [英] How to find out the running/start time of android application?

查看:212
本文介绍了如何找出运行/启动Android应用程序的时间呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有无论如何,找出应用程序的启动时间? 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.

推荐答案

这将返回过程的开始时间(从系统启动):

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

要获取进程运行时间:

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

这篇关于如何找出运行/启动Android应用程序的时间呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆