Skip to content Skip to sidebar Skip to footer

How To Find Out The Running/start Time Of Android Application?

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.

Solution 1:

This will return process start time (since system boot):

privatestaticlonggetStartTime(finalint pid)throws IOException {
    finalStringpath="/proc/" + pid + "/stat";
    finalBufferedReaderreader=newBufferedReader(newFileReader(path));
    final String stat;
    try {
        stat = reader.readLine();
    } finally {
        reader.close();
    }
    finalStringfield2End=") ";
    finalStringfieldSep=" ";
    finalintfieldStartTime=20;
    finalintmsInSec=1000;
    try {
        final String[] fields = stat.substring(stat.lastIndexOf(field2End)).split(fieldSep);
        finallongt= Long.parseLong(fields[fieldStartTime]);
        finalinttckName= Class.forName("libcore.io.OsConstants").getField("_SC_CLK_TCK").getInt(null);
        finalObjectos= Class.forName("libcore.io.Libcore").getField("os").get(null);
        finallongtck= (Long)os.getClass().getMethod("sysconf", Integer.TYPE).invoke(os, tckName);
        return t * msInSec / tck;
    } catch (final NumberFormatException e) {
        thrownewIOException(e);
    } catch (final IndexOutOfBoundsException e) {
        thrownewIOException(e);
    } catch (ReflectiveOperationException e) {
        thrownewIOException(e);
    }
}

To get process running time:

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

Solution 2:

I don't know wether there's an API for this. But one approach would be to use something like:

Stringpid="yourpid";    
BufferedReaderreader=newBufferedReader ( newInputStreamReader ( newFileInputStream ( "ls -ld /proc/"+pid)) , 1000 );

To get the start time of your application and the just subtract that with the currenttime.

Might not be the best way, but that's what came to mind. (And I haven't tried it..)

Gl !

Post a Comment for "How To Find Out The Running/start Time Of Android Application?"