Skip to content

Instantly share code, notes, and snippets.

@chenenyu
Created July 20, 2018 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chenenyu/e1b7c5baef5af4c7a2d7202ed8a002ad to your computer and use it in GitHub Desktop.
Save chenenyu/e1b7c5baef5af4c7a2d7202ed8a002ad to your computer and use it in GitHub Desktop.
获取应用启动时间
// 应用启动时间
private long getStartTime() throws Exception {
if (Build.VERSION.SDK_INT >= 24) {
return Process.getStartElapsedRealtime();
}
final String path = "/proc/" + Process.myPid() + "/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;
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;
}
// 应用已启动总时长(ms)
long bootTime = (SystemClock.elapsedRealtime() - startTime) / 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment