2011-04-05 61 views
3

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

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

也许你会发现helpfull:http://stackoverflow.com/questions/3677229 /如何找到运行的开始时间的Android应用程序 – MByD 2011-04-05 12:56:53

回答

0

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

+0

在RunningServiceInfo他们只有lastActivityTime。但我想要这个过程开始的时间。 lastActivityTime和流程开始时间是否相同? – 2011-04-05 13:08:34

+0

他们也有'activeSince',在回答中的链接 – WarrenFaith 2011-04-05 13:14:59

+0

activeSince在RunningServiceInfo中..看到我原来的问题。我正在询问RunningAppProcessInfo。 – 2011-04-05 14:49:55

2

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

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