【问题标题】:Running process start time运行进程开始时间
【发布时间】:2011-04-05 12:53:20
【问题描述】:

我正在使用下面的代码来获取设备上所有当前正在运行的进程。如何获取运行进程的开始时间?

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

【问题讨论】:

标签: android


【解决方案1】:

使用 kotlin 和 API 21,上面的代码变成了

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

【讨论】:

    【解决方案2】:

    以上答案的补充..

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

    【讨论】:

      【解决方案3】:

      这将返回进程开始时间(自系统启动以来):

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

      【讨论】:

      • 这在某些手机上失败了,因为Android N,issuetracker.google.com/issues/37091475,有没有新的方法?
      • 这并不是每次都为该过程提供正确的开始时间,尤其是对于三星设备。
      【解决方案4】:

      据我所知,您只能从服务中接收此信息。看看documentation of ActivityManager.RunningServiceInfoactiveSince attribute

      【讨论】:

      • 在 RunningServiceInfo 中他们只有 lastActivityTime。但我想要这个过程开始的时间。 lastActivityTime 和进程开始时间是否相同?
      • 他们也有activeSince,答案中的链接
      • activeSince 在 RunningServiceInfo.. 请参阅我原来的问题。我在问 RunningAppProcessInfo。
      • @Ajay 正如我的回答所提到的,只有一个可用于服务的信息,请阅读我的第一句话...
      猜你喜欢
      • 1970-01-01
      • 2021-05-09
      • 2015-08-21
      • 1970-01-01
      • 1970-01-01
      • 2021-05-11
      • 2021-05-20
      • 2011-08-15
      • 1970-01-01
      相关资源
      最近更新 更多