0

我正在尝试使用以下命令转换从 adb shell getevent 捕获的时间戳

adb shell getevent -lt /dev/input/event2 >filepath/filename.txt

这给出了如下所示的输出

  [   19393.303318] EV_ABS       ABS_MT_TRACKING_ID   00000b87
  [   19393.303318] EV_ABS       ABS_MT_POSITION_X    00000180
  [   19393.303318] EV_ABS       ABS_MT_POSITION_Y    000004e2
  [   19393.303318] EV_ABS       ABS_MT_PRESSURE      0000004c
  [   19393.303318] EV_ABS       ABS_MT_TOUCH_MAJOR   00000001
  [   19393.303318] EV_SYN       SYN_REPORT           00000000

这里看到的时间戳是 19393.303318 不是正常时间格式

如何使用 JAVA 将其转换为 hh:mm:ss:SSS 格式

如果可能,是否有任何其他方法可以为 adb shell getevent 获取正确的时间格式

提前致谢

4

3 回答 3

1

我终于发现 getevent 的输出是基于uptimeMillis()而不是elapsedRealtime(). 就像getEventTimehttps://developer.android.com/reference/android/view/InputEvent.html#getEventTime()

我使用这个 uptimeMillis 得到正确的时间戳。

于 2017-07-17T16:11:17.873 回答
-1

它看起来像自启动以来的秒数。要获得启动时刻的绝对时间戳,您可以使用以下内容:System.currentTimeMillis() - SystemClock.elapsedRealtime().

于 2015-12-20T23:03:38.453 回答
-1

这是你的答案。我认为您必须开始搜索您的查询。我不擅长java,但这花了我2个小时来搜索和写这个。无论如何都没有正确安排这工作,现在我可以使用它来将dmesg转换为adb shell日期格式。注意:- 虽然替换date and uptime使用 - date;cat /proc/uptime在 adb shell 中同时获取两者(不完全准确)

转换getevent文件的主要方法

public static void convertGetEventFiletoHumanReadableTime(
        String geteventFileLocation) throws ParseException, IOException {

    SimpleDateFormat adbDatefmt = new SimpleDateFormat(
            "EEE MMM dd hh:mm:ss z yyyy");
    String adbDate = "Mon Jan  4 21:23:19 KST 2016"; //replace with your adb shell date
    String uptime = "33224.56 242604.45" //replace with your device cat adb shell /proc/uptime
    uptime = uptime.split(" ")[0];
    String event = "/dev/input/event1" //replace with event type you need to

    BufferedReader br = new BufferedReader(
            new FileReader(dmesgFileLocation));
    try {
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();

        int lineNum = 0;
        while (line != null) {

            // System.out.println(lineNum + ": " + line); //for debug
            if (line.trim().length() > 0) {
                String eventTimeStamp = getEventTimeStamp(line, event);
                System.out.println("TIME " + eventTimeStamp);
                float eventTimeF = Float.parseFloat(eventTimeStamp);
                float eventTimeMillisecF = (float) (Math
                        .round(eventTimeF * 100.0) / 100.0);

                Date dmesghTime = convertGeteventTimestampToHumanReadable(
                        uptime, adbDate, eventTimeMillisecF);
                line = line.replace(eventTimeStamp,
                        adbDatefmt.format(dmesghTime));

                sb.append(line);
                sb.append(System.lineSeparator());
            }
            line = br.readLine();
            lineNum++;
        }
        String everything = sb.toString();
        // System.out.println(everything);
        try {
            FileWriter fw = new FileWriter("/getevent_humanTime.txt", true);
            fw.write(everything);
            fw.close();
        } catch (IOException e) {
            System.out.println("Something happened - here's what I know: ");
            e.printStackTrace();

        }
    } finally {
        br.close();
    }
}

从每一行获取事件时间戳的方法

public static String getEventTimeStamp(String event_string, String pattern) {

    Pattern pTime = Pattern
            .compile("\\[.*(\\d\\d\\d\\d\\d\\.\\d\\d\\d\\d\\d\\d)\\].*"
                    + pattern);
    Matcher mTime = pTime.matcher(event_string);

    String time = null;
    if (mTime.find()) {
        System.out.println("Time " + mTime.group(1));
        time = mTime.group(1);
    }
    return time;
}

将纪元时间转换为人类可读的方法 [round to millisec]

public static Date convertGeteventTimestampToHumanReadable(String  uptime,String adbDate , float secTimestamp)
        throws ParseException {


    System.out.println("DEVICE UPTIME IN SECS " + uptime);

    float uptimeMilliSecF = Float.parseFloat(uptime) * 1000;
    int uptimeMilliSec = Float.valueOf(uptimeMilliSecF).intValue();

    int dmesgtimeinMillisec = (int) (secTimestamp * 1000);

    SimpleDateFormat adbDatefmt = new SimpleDateFormat(
            "EEE MMM dd hh:mm:ss z yyyy");
    Date date = adbDatefmt.parse(adbDate);
    System.out.println("ADB DATE: " + date);

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);

    calendar.add(Calendar.MILLISECOND, -uptimeMilliSec);
    System.out.println("ADB DATE AT DEVICE UP: " + calendar.getTime());

    calendar.add(Calendar.MILLISECOND, dmesgtimeinMillisec);
    System.out.println("ADB DATE FOR DMESG EVENT: " + calendar.getTime());

    return calendar.getTime();

}
于 2016-01-04T12:55:11.050 回答