您可以使用
stat或
lstat获得最后一次访问时间.两种方法
android.system.Os.stat(String path)和
android.system.Os.lstat(String path)在Android 5.0中公布.在以前的Android版本中,您将需要使用反射或在shell中运行命令.
用法:
Android 5.0
long lastAccessTime = Os.lstat(file.getAbsolutePath()).st_atime;
在Android 5.0之前使用反射
Class> clazz = Class.forName("libcore.io.Libcore");
Field field = clazz.getDeclaredField("os");
if (!field.isAccessible()) {
field.setAccessible(true);
}
Object os = field.get(null);
Method method = os.getClass().getMethod("lstat", String.class);
Object lstat = method.invoke(os, file.getAbsolutePath());
field = lstat.getClass().getDeclaredField("st_atime");
if (!field.isAccessible()) {
field.setAccessible(true);
}
long lastAccessTime = field.getLong(lstat);
注意:
我不认为Android上使用最后访问时间.从java.nio文档:
If the file system implementation does not support a time stamp to indicate the time of last access then this method returns an implementation specific default value, typically the last-modified-time or a FileTime representing the epoch (1970-01-01T00:00:00Z).
我测试了使用以下命令更改最后访问时间:
touch -a [PATH]
这确实改变了以root用户身份运行命令的最后访问时间.不过,我认为最后访问的时间不会在Android上更新/使用.