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.ACTIVITY_SERVICE);
process = activityMan.getRunningAppProcesses();
for (Iterator iterator = process.iterator(); iterator.hasNext();) {
RunningAppProcessInfo runningAppProcessInfo = (RunningAppProcessInfo) iterator
开发者_开发问答 .next();
pSname= runningAppProcessInfo.processName;
System.out.println(pSname);
}
This will return process start time (since system boot):
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);
}
}
To get process running time:
final long dt = SystemClock.elapsedRealtime() - getStartTime(Process.myPid());
With kotlin and API 21 the above code becomes
@Throws(IOException::class)
private fun getStartTime( 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);
}
}
supplement for the above answer..
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);
}
}
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
精彩评论