2011-04-05 53 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); 
    } 
}