Arl*_*ind 27 linux android ntp huawei
问题是System.currentTimeMillis()
返回错误的毫秒,不同的时间范围主要在未来有时长达6个月,但它从几秒到几个月不等.
发生这种情况的设备是Android 5.1.1上的平板型号华为M2-A201W内核版本是:**3.10.74-gdbd9055**
我的第一个假设是NTP在某种程度上搞乱了时间,但我有数千台平板电脑,其中一些没有网络连接,没有SIM卡,所以没有GSM/3G/4G.
我System.currentTimeMillis()
在一个列中保存了一个表,以便在本地sqlite数据库中创建一行.
System.currentTimeMillis()
在我使用的平板电脑上,这种异常频繁发生(每次通话的30%).
作为使用的解决方法System.currentTimeMillis()
,也许你可以让sqlite处理时间戳创建,看看是否能解决你的问题?
如果您需要毫秒timestamp default current_timestamp
,default(strftime('%Y-%m-%d %H:%M:%f', 'now'))
则使用或使用或定义/创建"已创建"列,如下所示:
sqlite> create table my_table(id integer primary key autoincrement not null, name text, created timestamp default(strftime('%Y-%m-%d %H:%M:%f', 'now')) not null);
sqlite> insert into my_table(name) values ('MyTestRow1');
sqlite> insert into my_table(name) values ('MyTestRow2');
sqlite> select * from my_table;
1|MyTestRow1|2017-08-07 10:08:50.898
2|MyTestRow2|2017-08-07 10:08:54.701
System.currentTimeMillis()
Android平台中的Java API调用使用POSIX api gettimeofday
以毫秒为单位获取时间.看到这里.
static jlong System_currentTimeMillis(JNIEnv*, jclass) {
timeval now;
gettimeofday(&now, NULL);
jlong when = now.tv_sec * 1000LL + now.tv_usec / 1000;
return when;
}
Run Code Online (Sandbox Code Playgroud)
它假设每次通话gettimeofday
都会成功.我想你的问题可能会在这里发生.
最好检查每个API调用的返回值,并确定错误发生时下一步该做什么.
因此,我建议使用您自己的实现在JNI中使用更可靠的方法,如下所示.按顺序调用这些POSIX API,如果gettimeofday
失败,则调用clock_gettime
,如果再次失败,则调用time
.
struct timeval now;
if (gettimeofday(&now, NULL) != 0) {
struct timespec ts;
if (clock_gettime(CLOCK_REALTIME, &ts) == 0) {
now.tv_sec = ts.tv_sec;
now.tv_usec = ts.tv_nsec / 1000LL;
} else {
now.tv_sec = time(NULL);
now.tv_usec = 0;
}
}
jlong when = now.tv_sec * 1000LL + now.tv_usec / 1000LL;
__android_log_print(ANDROID_LOG_INFO, "TAG", "%lld", when);
Run Code Online (Sandbox Code Playgroud)
当您没有SIM卡而没有GSM/3G/4G时,您的手机无法根据网络提供的时间/区域更新正确的时间.
因此,具有网络的设备显示正确的时间,而没有网络的其他设备可能显示不正确的时间 - 您必须手动设置正确的时间.System.currentTimeMilis()从系统中读取时间.g但是在电源转动时,时钟工作.
检查使用Socket或DatagramSocket的应用程序是否阻止了NTP(UDP端口123).注意:NTP适用于网络中所有主机或路由器的时钟必须相同的场景.如果您的设备切换到两个(或更多)不同的网络并从不同的来源获得时间更新,则可能会有时间波动.
最终,您的系统时间正在改变,这就是它波动的原因.如果手动禁用自动日期和时间后手动System.currentTimeMilis(),我相信,它不会波动(没有异常).如果是这种情况,那么您的Huewai平板电脑没有错误.
归档时间: |
|
查看次数: |
2543 次 |
最近记录: |