正在运行的进程的开始时间 [英] Running process start time

查看:113
本文介绍了正在运行的进程的开始时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面code让所有正在运行的进程的器件。我怎样才能运行的进程的开始时间?

  activityMan =(ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
    过程= activityMan.getRunningAppProcesses();
    对于(迭代器迭代器= process.iterator(); iterator.hasNext()){
        RunningAppProcessInfo runningAppProcessInfo =(RunningAppProcessInfo)迭代器
                。下一个();
        pSname = runningAppProcessInfo.processName;
        的System.out.println(pSname);
    }
 

解决方案

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

 私有静态长getStartTime(最终诠释PID)抛出IOException异常{
    最后弦乐路径=的/ proc /+ PID +/ STAT;
    最后的BufferedReader读卡器=新的BufferedReader(新的FileReader(路径));
    最后弦乐统计;
    尝试 {
        STAT = reader.readLine();
    } 最后 {
        reader.close();
    }
    最后弦乐field2End =);
    最后弦乐fieldSep =;
    最终诠释fieldStartTime = 20;
    最终诠释msInSec = 1000;
    尝试 {
        。最终的String []字段= stat.substring(stat.lastIndexOf(field2End))分裂(fieldSep);
        最终的长T =的Long.parseLong(场[fieldStartTime]);
        最终诠释tckName =的Class.forName(libcore.io.OsConstants)getfield命令(_ SC_CLK_TCK)调用getInt(空)。
        最终对象OS =的Class.forName(libcore.io.Libcore)getfield命令(OS)获得(空)。
        最终长TCK =(长)os.getClass()实现getMethod(的sysconf,Integer.TYPE).invoke(OS,tckName)。
        返回T * msInSec / TCK;
    }赶上(最终NumberFormatException异常E){
        抛出新IOException异常(E);
    }赶上(最终IndexOutOfBoundsException异常E){
        抛出新IOException异常(E);
    }赶上(ReflectiveOperationException E){
        抛出新IOException异常(E);
    }
}
 

要获取进程运行时间:

 最后长DT = SystemClock.elapsedRealtime() -  getStartTime(Process.myPid());
 

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

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

解决方案

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

这篇关于正在运行的进程的开始时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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