2011-04-05 8 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

아마도 도움이 될 것입니다. http : //stackoverflow.com/questions/3677229/running-start-of-the-run-start-of-the-run-start-of-theroid 응용 프로그램을 찾으십시오. – MByD

답변

0

알고있는 한이 정보는 서비스에서만받을 수 있습니다. (시스템 부팅 이후) 프로세스 시작 시간을 반환합니다 documentation of ActivityManager.RunningServiceInfo 또는 activeSince attribute

+0

RunningServiceInfo에는 lastActivityTime 만 있습니다. 그러나 나는이 과정이 시작될 시간을 원한다. lastActivityTime과 프로세스 시작 시간이 동일합니까? –

+0

그들은 또한'activeSince'을 가지고 있습니다. 대답은 – WarrenFaith

+0

입니다. activeSince는 RunningServiceInfo에 있습니다. 내 원래 질문을보십시오. RunningAppProcessInfo에 대해 묻습니다. –

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