我正在尝试在 C 语言的 NDK native 代码中设置设备时间。据我所知,最好的解决方案是使用 settimeofday()。
struct timeval tv = {timet, 0};
struct timezone tz = {timezone, daylight}; // The second timezone is long int. Time lib extern which perfectly suits here.
if (settimeofday(&tv, &tz) != 0)
{
LOG(("settimeofday() failed: %s", strerror(errno)));
}
结果在
settimeofday() failed: Read-only file system
native中还有其他设置时间的方法吗?有可能吗?我查看了问题的只读文件系统部分,但我没有 Root 设备。
提前致谢!
最佳答案
您不能在没有足够权限的情况下设置设备时间,并且应用程序将无法在未获得 root 权限的设备上获得该权限。从手册页:
EPERM The calling process has insufficient privilege to call settime-
ofday(); under Linux the CAP_SYS_TIME capability is required.
即使您设法设置了它,下次设备通过网络更新时间时它也可能会被覆盖。
您获得的是 EROFS 而不是 EPERM,这有点奇怪。
关于c - Android 上的 settimeofday() 导致 errno - 只读文件系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18873927/