Last active
September 17, 2022 12:13
-
-
Save BCsl/dd30f8df6fe76b346b12295451aefbf6 to your computer and use it in GitHub Desktop.
NTP 网络时间
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by chensuilun on 2017/7/4. | |
*/ | |
public class NTUSUtils { | |
private static final String TAG = "NTUSUtils"; | |
private static final String NTP_TRUSTED_TIME = "android.util.NtpTrustedTime"; | |
private static final String GET_INSTANCE = "getInstance";//private NtpTrustedTime getInstance(Context context) | |
private static final String FORCE_REFRESH = "forceRefresh"; //public boolean forceRefresh() | |
private static final String GET_CACHED_NTP_TIME = "getCachedNtpTime";//public long getCachedNtpTime() | |
private static Context sContext = AppApplication.getContext(); | |
private static Method sGetNtpTimeMethod; | |
private static Method sForceRefreshMethod; | |
private static Class sNTPTrustedTimeClz; | |
private static Object sNtpTrustedTimeInstance; | |
static { | |
try { | |
sNTPTrustedTimeClz = Class.forName(NTP_TRUSTED_TIME); | |
Method instanceMethod = sNTPTrustedTimeClz.getDeclaredMethod(GET_INSTANCE, Context.class); | |
instanceMethod.setAccessible(true); | |
sNtpTrustedTimeInstance = instanceMethod.invoke(null, sContext); | |
sGetNtpTimeMethod = sNTPTrustedTimeClz.getDeclaredMethod(GET_CACHED_NTP_TIME); | |
sForceRefreshMethod = sNTPTrustedTimeClz.getDeclaredMethod(FORCE_REFRESH); | |
} catch (ClassNotFoundException e) { | |
e.printStackTrace(); | |
} catch (NoSuchMethodException e) { | |
e.printStackTrace(); | |
} catch (InvocationTargetException e) { | |
e.printStackTrace(); | |
} catch (IllegalAccessException e) { | |
e.printStackTrace(); | |
} | |
} | |
@WorkerThread | |
public static boolean forceFresh() { | |
if (BuildConfig.DEBUG) { | |
Log.d(TAG, "forceFresh() called with: " + ""); | |
} | |
if (sForceRefreshMethod != null) { | |
try { | |
return (boolean) sForceRefreshMethod.invoke(sNtpTrustedTimeInstance); | |
} catch (IllegalAccessException e) { | |
e.printStackTrace(); | |
} catch (InvocationTargetException e) { | |
e.printStackTrace(); | |
} | |
} | |
return false; | |
} | |
public static long getCachedNtpTime() { | |
if (BuildConfig.DEBUG) { | |
Log.d(TAG, "getCachedNtpTime() called with: " + ""); | |
} | |
if (sGetNtpTimeMethod != null) { | |
try { | |
return (long) sGetNtpTimeMethod.invoke(sNtpTrustedTimeInstance); | |
} catch (IllegalAccessException e) { | |
e.printStackTrace(); | |
} catch (InvocationTargetException e) { | |
e.printStackTrace(); | |
} | |
} | |
return -1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
是调用forceFresh这个方法么 我调用了 没作用