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