Android Question Getting syncronized Date & Time

trueboss323

Active Member
Licensed User
Longtime User
I have an app that requires periodically checking the date and time server to make sure the date and time are correct. But at the same time, I do not want the user to manually change the date/time in their device settings. I think the best way I could do this is check and see if the "Automatic date & time" option is enabled in settings. Are there any other ways I could do this and what kind of code modules I could use? Please and thanks.
 

Ed Brown

Active Member
Licensed User
Longtime User
Just a thought...

You could periodically take the difference between the time in the server and in the device (mobile/tablet/Pi/etc). With this difference in time you would only need to add it to the time on the device. This way, the user does not need to change the time on the device.
 
Upvote 0

trueboss323

Active Member
Licensed User
Longtime User
While the examples are great, I would also like it if it used little to no data. Checking the date & time from the server I assume would use some data. That is why I suggested checking to see if that option was enabled in settings would be better.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use this code:
B4X:
Sub CheckAutoTime As Boolean
   Dim p As Phone
   Dim s As String
   If p.SdkVersion >= 17 Then s = "android.provider.Settings.Global" Else  s = "android.provider.Settings.System"
   Dim ctxt As JavaObject
   ctxt.InitializeContext
   Dim jo As JavaObject
   Return jo.InitializeStatic(s).RunMethod("getInt", Array(ctxt.RunMethod("getContentResolver", Null), "auto_time", 0)) = 1
End Sub
Based on this answer: http://stackoverflow.com/questions/23075446/how-to-check-automatic-date-and-time-is-enabled-or-not
 
Upvote 0
Top