Sntp
是参照android.util.NtpTrustedTime
、android.net.SntpClient
重构的可 自定义NTP服务器地址 的工具类,其中 SntpClient
实现NTP时间同步。
allprojects {
repositories {
google()
jcenter()
maven {
url 'https://dl.bintray.com/cvdnn/maven'
}
}
}
dependencies {
implementation 'com.cvdnn:edge-ntp:0.5.3'
implementation 'android.cvdnn:android-lang:0.3.25'
implementation 'com.squareup.okio:okio:1.14.0'
}
- 设置自定义NTP服务器
Sntp.Impl.setNtpAddress(ntpAddress)
- 时间同步和设置系统时间
boolean ntpResult = Sntp.Impl.forceRefresh();
if (ntpResult) {
mCmdResult = Sntp.Impl.setTimeMillis();
}
forceRefresh()
时会执行网络操作,故必须在子线
程中执行- Lib中是通过
date -s yyyyMMdd.HHmmss
的方式设置已 ROOT 的系统;