package se.nekotronic.simplegpstest; import android.Manifest; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; import java.util.Date; import java.text.SimpleDateFormat; import android.util.Log; import android.location.Location; import android.location.LocationManager; import android.location.LocationListener; import android.content.Context; import java.text.DecimalFormat; public class MainActivity extends AppCompatActivity { private TextView texten = null; private LocationManager locationManager; MyLocationListener locationListener; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Acquire a reference to the TextView we print in texten = (TextView) findViewById(R.id.textView1); texten.setText(""); print("onCreate"); // Acquire a reference to the system Location Manager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); locationListener = new MyLocationListener(); Location location = getLatestKnownLocation(); if (location == null) print("No latest known location."); else { double latitude = location.getLatitude(); double longitude = location.getLongitude(); double accuracy = location.getAccuracy(); double altitude = location.getAltitude(); print("Latest known location from " + dateFormat.format(location.getTime()) + ": Long " + decimalFormat.format(longitude) + ", lat " + decimalFormat.format(latitude) + ", accuracy " + decimalFormat.format(accuracy) + ", alt " + decimalFormat.format(altitude)); } } // onCreate @Override protected void onPause() { super.onPause(); print("onPause"); // Here in onPause, we cancel the subscription to location updates locationManager.removeUpdates(locationListener); } @Override protected void onResume() { super.onResume(); print("onResume"); // Here in onResume, we subscribe to location updates if (android.os.Build.VERSION.SDK_INT >= 23) { print("Detta är en nyare Android (API " + android.os.Build.VERSION.SDK_INT + "), så appen måste begära GPS-rättigheter av användaren."); int hasPermission = this.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION); if (hasPermission == android.content.pm.PackageManager.PERMISSION_GRANTED) print("Har redan GPS-rättigheter."); else this.requestPermissions((new String[]{Manifest.permission.ACCESS_FINE_LOCATION}), 4711); } else { print("Detta är en äldre Android (API " + android.os.Build.VERSION.SDK_INT + "), så GPS-rättigheterna i AndroidManifest.xml räcker."); } try { // Register the listener with the Location Manager to receive location updates locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); } catch (SecurityException e) { print("requestLocationUpdates failed because missing permissions: " + e.getMessage()); } } private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); private void print(String message) { Date now = new Date(); String nowString = dateFormat.format(now); String line = nowString + ": " + message; texten.setText(texten.getText() + line + "\n"); Log.d("**************", line); texten.invalidate(); texten.postInvalidate(); } private DecimalFormat decimalFormat = new DecimalFormat("0.0000"); // Define a listener that responds to location updates private class MyLocationListener implements LocationListener { // Called when a new location is found by the network location provider public void onLocationChanged(Location location) { double latitude = location.getLatitude(); double longitude = location.getLongitude(); double accuracy = location.getAccuracy(); double altitude= location.getAltitude(); print("Received a GPS location: Long " + decimalFormat.format(longitude) + ", lat " + decimalFormat.format(latitude) + ", accuracy " + decimalFormat.format(accuracy) + ", alt " + decimalFormat.format(altitude)); } public void onStatusChanged(String provider, int status, Bundle extras) { print("GPS provider status changed to " + status); } public void onProviderEnabled(String provider) { print("GPS provider enabled"); } public void onProviderDisabled(String provider) { print("GPS provider disabled"); } } // MyLocationListener // Find the latest known location private Location getLatestKnownLocation() { try { Location locationFromGPS = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); Location locationFromNetwork = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); long GPSLocationTime = 0; if (locationFromGPS == null) return locationFromNetwork; else if (locationFromNetwork == null) return locationFromGPS; else if (locationFromNetwork.getTime() > locationFromGPS.getTime()) return locationFromNetwork; else return locationFromGPS; } catch (SecurityException e) { print("getLastKnownLocation failed because missing permissions: " + e.getMessage()); return null; } } // getLatestKnownLocation } // MainActivity