Running Process Start Time
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.ACTIVI
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:
supplement for the above answer..
privatestatic long getProcessStartTime(finalint pid) throws Exception {
finalString path = "/proc/" + pid + "/stat";
final BufferedReader reader = new BufferedReader(new FileReader(path));
finalString stat;
try {
stat = reader.readLine();
} finally {
reader.close();
}
finalString field2End = ") ";
finalString fieldSep = " ";
finalint fieldStartTime = 20;
finalint msInSec = 1000;
try {
finalString[] 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);
}
finalObject 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) {
thrownewException(e);
}
}
Solution 3:
With kotlin and API 21 the above code becomes
@Throws(IOException::class)privatefungetStartTime( pid:Int ) : Long {
val reader = BufferedReader(FileReader ("/proc/$pid/stat"));
val stats = try {
reader.readLine();
} finally {
reader.close();
}
val fieldStartTime = 20;
val msInSec = 1000;
try {
val fields = stats.substring (stats.lastIndexOf(") ")).split(" ");
val t = fields[fieldStartTime].toLong();
val tck = Os.sysconf(OsConstants._SC_CLK_TCK);
return (t * msInSec) / tck;
} catch (e: NumberFormatException) {
throw IOException (e);
} catch (e: IndexOutOfBoundsException ) {
throw IOException (e);
}
}
Solution 4:
As far as I know you can only receive this information from a service. Take a look at the documentation of ActivityManager.RunningServiceInfo or activeSince attribute
Post a Comment for "Running Process Start Time"