Simple Network Time Protocol client for Java and Android.
SNTP.setClient()initializes global staticSNTPClientSNTP.setCache()initializes global staticSNTPCacheSNTP.currentTimeMillis()returns current global time. It may use netorking and can throwIOException. Do not call it on main event thread.SNTP.safeCurrentTimeMillis()same as above but will not throw anything and will useSystem.currentTimeMillis()as fallback.
In your build.gradle:
repositories {
maven { url 'https://raw.githubusercontent.com/eterverda/sntp/m2/' }
}
dependencies {
compile 'io.github.eterverda.sntp:sntp-android:0.2.2'
}
In onCreate of your application:
SNTP.setClient(AndroidSNTPClientFactory.create());
SNTP.setCache(AndroidSNTPCacheFactory.create(this));
... or you can extend SNTPApplication.
In your AndroidManifest.xml add:
<receiver
android:name="io.github.eterverda.sntp.android.SNTPResetCacheReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.TIME_SET"/>
</intent-filter>
</receiver>
... to reset SNTP cache when user changes time in phone settings.
In build.gradle of your Java project:
repositories {
maven { url 'https://raw.githubusercontent.com/eterverda/sntp/m2/' }
}
dependencies {
compile 'io.github.eterverda.sntp:sntp:0.2.2'
}
Somehere on startup of application:
SNTP.init()
... however this will not create persistent SNTP cache. If you need one replace SNTP.init() with:
SNTP.setClient(SNTPClientBuilder.create());
SNTP.setCache(SNTPCacheBuilder.custom().setFile(sntpCacheFile).build());