3

I have an app that runs offline. I need to access the current time and since the time can be changed by the user and internet is not available so I want to get the time provided by the network.

I tried to use this

LocationManager locMan = (LocationManager) Activity.getSystemService(Activity.LOCATION_SERVICE);
long networkTime = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER).getTime();

But this also returns the changed date/time, not the current time.

I also tried to enforce the Network provided date and time but it is not working. I am doing this

String timeSettings = android.provider.Settings.System.getString(
                this.getContentResolver(),
                android.provider.Settings.System.AUTO_TIME);
        if (timeSettings.contentEquals("0")) {
            android.provider.Settings.System.putString(
                    this.getContentResolver(),
                    android.provider.Settings.System.AUTO_TIME, "1");
        }

This is also not enforcing the Network provided date and time. Since internet is not available so I can't use NTP as well. Is there any way I can get the current time (even if its few hours out of sync i.e. last known location time).

Thank you, Hamza

Hamza
  • 1,593
  • 2
  • 19
  • 31
  • Since your app works offline, using network-supplied time sources seems pointless, as you do not have a network connection. If your first sentence of your question is inaccurate, then you are welcome to use an NTP client of your own to get the UTC time from some time server. – CommonsWare Dec 04 '15 at 20:29
  • http://stackoverflow.com/questions/8049912/how-can-i-get-the-network-time-from-the-automatic-setting-called-use-netw – marcinj Dec 04 '15 at 20:32
  • @CommonsWare No its the time provided my Network Operator not the wifi network. For example with data connection and wifi off, If I select "Use Network provided date and time", it updates the time.. I am asking for that time.. or if somehow I can select Use Network provided date and time" – Hamza Dec 04 '15 at 23:22
  • "its the time provided my Network Operator not the wifi network" -- first, not all network operators provide time signals (e.g., NITZ) AFAIK. Second, you have no way of interacting with that anyway. Either the device is getting time signals and applying them (because the user opted into that and those signals are available), or the device is not. You can't affect that one way or another. – CommonsWare Dec 04 '15 at 23:44

0 Answers0