-
Notifications
You must be signed in to change notification settings - Fork 195
How to use this library
Kaushik Gopal edited this page Sep 5, 2018
·
5 revisions
We use Jitpack to host the library.
Add this to your application's build.gradle
file:
repositories {
maven {
url "https://jitpack.io"
}
}
dependencies {
// ...
compile 'com.github.instacart.truetime-android:library-extension-rx:<release-version>'
// or if you want the vanilla version of Truetime:
compile 'com.github.instacart.truetime-android:library:<release-version>'
}
Importing 'com.github.instacart.truetime-android:library:<release-version>'
should be sufficient for this.
Then you must initialize it in onCreate()
in your class that extendsandroid.app.Application
.
TrueTime.build().initialize();
initialize
also must be run on a background thread. If you run it on the main thread, you will get a NetworkOnMainThreadException
You can then use:
Date noReallyThisIsTheTrueDateAndTime = TrueTime.now();
... #winning
If you're down to using RxJava then we go all the way and implement the full NTP. Use the nifty initializeRx()
api which takes in an NTP pool server host.
Again, you must initialize it in onCreate()
in your class that extendsandroid.app.Application
.
TrueTimeRx.build()
.initializeRx("time.google.com")
.subscribeOn(Schedulers.io())
.subscribe(date -> {
Log.v(TAG, "TrueTime was initialized and we have a time: " + date);
}, throwable -> {
throwable.printStackTrace();
});
Now, as before:
TrueTimeRx.now(); // return a Date object with the "true" time.