1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| public static final String URL_CHECK_TIME = "http://www.taobao.com"; private static boolean useLocal; private static long deltaTime;
public static void resetTimeStamp() { useLocal = false; deltaTime = 0; }
public static String getTimeStamp() { final long phoneTime = System.currentTimeMillis() / 1000;
String result = String.valueOf(phoneTime); if (!useLocal) { new Thread(new Runnable() { @Override public void run() { try { URLConnection uc = new URL(URL_CHECK_TIME).openConnection(); uc.connect(); long webSiteTime = uc.getDate() / 1000;
long currentTime = System.currentTimeMillis() / 1000; useLocal = Math.abs(currentTime - webSiteTime) < 5 * 60; deltaTime = currentTime - webSiteTime;
} catch (Exception e) { Log.e(TAG, "getTimeStamp: ", e); resetTimeStamp(); }
} }).start();
result = String.valueOf(phoneTime - deltaTime); }
Log.i(TAG, "getTimeStamp: 本次使用了本机时间?" + useLocal); return result; }
|