5

As per question stated I need actual time. For example in my app I have to show a specific pop up to all the users at 8 AM national time. App runs in a specific country. What if there device time is incorrect ? I can get the time by an api . What if device don't have interenet ? I was thinking about taking the track of time when ever I got internet. For example, If I got internet I hit an api to get the time and store that time to keep track of it. Any other sloution ?

Nouman Ghaffar
  • 3,780
  • 1
  • 29
  • 37
  • I don't think there is any solution to this situation. Also in this case not only yours but every app dependent on time will not function properly. – Vivek Mishra Sep 14 '17 at 12:27
  • 1
    do you really want to use magic outside the Hogwarts? Aren't you afraid if dementors? What source do you want to get time from? using what channel? You can't just get correct time in magical way. – Vladyslav Matviienko Sep 14 '17 at 12:34
  • @VladMatvienko hahaha. The batter answer would be get gps time, get time from sime, keep track of the time after specific interval. I was looking for a programmer with a logical answer. But luckly I found a Harry potter fan here. – Nouman Ghaffar Sep 14 '17 at 12:45
  • That was my way to say that you should define your requirements in more correct way. – Vladyslav Matviienko Sep 14 '17 at 12:47

3 Answers3

2

What about this solution: GPS-time in Android?

This will require the GPS receiver though.

Tim
  • 41,901
  • 18
  • 127
  • 145
Tjaart
  • 496
  • 8
  • 20
1

You can't get the real timestamp if your the Time set in the device is wrong..

Edit:- There is only a solution,I could found using phone internet is:-

You can give time from public server time. remember to add INTERNET permission in manifest.

String TIME_SERVER = "url"; 
NTPUDPClient timeClient = new NTPUDPClient(); 
InetAddress inetAddres=InetAddres.getByName(TIME_SERVER);
TimeInfo timeInfo = timeClient.getTime(inetAddress);
long returnTime = timeInfo.getMessage().getTransmitTimeStamp().getTime(); 
Date time = new Date(returnTime);
Shivam Sharma
  • 1,015
  • 11
  • 19
0

You'd need to use UTC. If you really, really need this I would have the app keep record of the time as UTC at the moment of installation (you know the user has internet at this time, so you can take advantage of that) and then have the app keep track of this UTC internally.

You could add seconds to the UTC as they pass while the app is open, and then localizing that time is trivial. You could then use the devices time to see how long it has been since the app was last opened (you'd need to create a timestamp each time the app closes and opens) and convert that time to seconds and add it to your apps stored UTC. Again, localizing this time is a triviality. I would also check if the user has internet any time they open the app and if they do have internet, get the correct UTC and update your updates internal UTC.

If you can get permission to run silently and count seconds in the background this becomes even easier, and you can give real time notifications. It's not perfect but I'm certain it could work.

Robbie Milejczak
  • 5,664
  • 3
  • 32
  • 65
  • The idea of getting initial time record and then using local loop interval to count it yourself, seems ok, bet there are problems. First, if I download the app, turn off the internet, run the app - there is no way to get that initial time record now. Second, if there is no internet, we can't use devices time, because it may be wrong (assume it's always wrong). – Starwave Jul 02 '20 at 08:16
  • The solution from my perspective - if the apps purpose is to do something at a specific time AND there is user authentication required, THEN we will 100% have internet at login moment, so that way we solved 1st problem, we now have initial correct time record from internet. As for the second issue, it is possible to create a background service, that would always be alive and it's only job would be counting seconds in interval (postDelayed or other method), that would work. However, instantiating a whole background service, even a small one, is a heavy thing, don't wanna go that way. – Starwave Jul 02 '20 at 08:21