GPS time in Android

I am looking for a way to display GPS time in my application. What is the easiest way to do this?

(This should be GPS time due to slight time differences between gps time and internal system time)

thanks

+8
android
source share
2 answers

Whenever you get a fix from GPS, the onLocationChanged method is onLocationChanged . The parameter of this method is an instance of Location . One method for this parameter is getTime , which will give you what you are looking for. That is, if I understood correctly what you are looking for. More here

+6
source share

Getting GPS time can be quite confusing! To expand the discussion in the accepted answer, getTime () in the onLocationChanged () callback gives different answers depending on how the location (not necessarily GPS) information is retrieved (based on Nexus 5 testing):

(a) If you use Google FusedLocationProviderApi (the Google geolocation services API), getProvider () will return 'fused' and getTime () will return the device time (System. currentTimeMillis ())

(b) When using the Android LocationManager (Android Location API), depending on the phone’s location settings and requestLocationUpdates (LocationManager.NETWORK_PROVIDER and / or LocationManager.GPS_PROVIDER) settings, getProvider () will return:

  • Or "network", in which case getTime () will return the device time (System.currentTimeMillis ()).
  • Or "gps", in which case getTime will return the GPS time (satellite).

Essentially: β€œfused” uses GPS and Wi-Fi / network, β€œnetwork” uses Wi-Fi / network, β€œgps” uses GPS.

Thus, to get GPS time, use the Android LocationManager with the requestLocationUpdates parameter set to LocationManager.GPS_PROVIDER. (Note that in this case, the millisecond part of getTime () is always 000)

Here is an example of using the Android LocationManager (Android Location API):

 public void InitialiseLocationListener(android.content.Context context) { android.location.LocationManager locationManager = (android.location.LocationManager) context.getSystemService(android.content.Context.LOCATION_SERVICE); android.location.LocationListener locationListener = new android.location.LocationListener() { public void onLocationChanged(android.location.Location location) { String time = new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SSS").format(location.getTime()); if( location.getProvider().equals(android.location.LocationManager.GPS_PROVIDER)) android.util.Log.d("Location", "Time GPS: " + time); // This is what we want! else android.util.Log.d("Location", "Time Device (" + location.getProvider() + "): " + time); } public void onStatusChanged(String provider, int status, android.os.Bundle extras) { } public void onProviderEnabled(String provider) { } public void onProviderDisabled(String provider) { } }; if (android.support.v4.content.ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION) != android.content.pm.PackageManager.PERMISSION_GRANTED) { android.util.Log.d("Location", "Incorrect 'uses-permission', requires 'ACCESS_FINE_LOCATION'"); return; } locationManager.requestLocationUpdates(android.location.LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener); locationManager.requestLocationUpdates(android.location.LocationManager.GPS_PROVIDER, 1000, 0, locationListener); // Note: To Stop listening use: locationManager.removeUpdates(locationListener) } 
+14
source share

All Articles