阿里云提供了7个NTP时间服务器也就是Internet时间同步服务器地址
ntp1.aliyun.com
ntp2.aliyun.com
ntp3.aliyun.com
ntp4.aliyun.com
ntp5.aliyun.com
ntp6.aliyun.com
ntp7.aliyun.com
需要frameworks_6.0_copy\base\core\java\android\net\SntpClient.java
static final int NTP_TIMEOUT_10_S = 10000;
static final String[] mNtpServers = { "cn.pool.ntp.org", "ntp1.aliyun.com", "ntp2.aliyun.com", "ntp3.aliyun.com",
"ntp4.aliyun.com", "ntp5.aliyun.com", "ntp6.aliyun.com", "ntp7.aliyun.com", };
void syncTime() {
SntpClient client = new SntpClient();
for (int i = 0; i < mNtpServers.length; i++) {
boolean flag = client.requestTime(mNtpServers[i], NTP_TIMEOUT_10_S);
if (flag) {
long now = client.getNtpTime() + SystemClock.elapsedRealtime() - client.getNtpTimeReference();
SystemClock.setCurrentTimeMillis(now);
break;
}
}
}
public static void startSyncTimeThread(Context context) {
Log.i(TAG, "ntp---startSyncTimeThread.");
boolean autoUpdateTimeEnable = getAutoState(context, Settings.Global.AUTO_TIME);
if (!autoUpdateTimeEnable) {
return;
}
new Thread() {
@Override
public void run() {
// TODO Auto-generated method stub
Log.i(TAG, "ntp---thread run.");
syncTime();
}
}.start();
}
© 版权声明