阿里云提供了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

  1. static final int NTP_TIMEOUT_10_S = 10000;
  2. static final String[] mNtpServers = { "cn.pool.ntp.org", "ntp1.aliyun.com", "ntp2.aliyun.com", "ntp3.aliyun.com",
  3. "ntp4.aliyun.com", "ntp5.aliyun.com", "ntp6.aliyun.com", "ntp7.aliyun.com", };
  4. void syncTime() {
  5. SntpClient client = new SntpClient();
  6. for (int i = 0; i < mNtpServers.length; i++) {
  7. boolean flag = client.requestTime(mNtpServers[i], NTP_TIMEOUT_10_S);
  8. if (flag) {
  9. long now = client.getNtpTime() + SystemClock.elapsedRealtime() - client.getNtpTimeReference();
  10. SystemClock.setCurrentTimeMillis(now);
  11. break;
  12. }
  13. }
  14. }
复制
  1. public static void startSyncTimeThread(Context context) {
  2. Log.i(TAG, "ntp---startSyncTimeThread.");
  3. boolean autoUpdateTimeEnable = getAutoState(context, Settings.Global.AUTO_TIME);
  4. if (!autoUpdateTimeEnable) {
  5. return;
  6. }
  7. new Thread() {
  8. @Override
  9. public void run() {
  10. // TODO Auto-generated method stub
  11. Log.i(TAG, "ntp---thread run.");
  12. syncTime();
  13. }
  14. }.start();
  15. }
复制

 

 

相关文章

暂无评论

none
暂无评论...