1

New to android... I am using Android Studio 1.0.2 with API 19 KitKat emulator (and project) to test gps handling... (also tested on phone with same results)

Thank you for any help! :)

telnet --> geo fix 24 24 does change locationManager.getLastKnownLocation! but does not fire onlocationchanged? (i never get the "triggered" log in the logcat where the getlastknownlocation is logged)

output: Latitude: 24.0. Longitude: 24.0. Provided by: gps at 1423821600000 (I know i need formatted date instead of gettime(), more worried about onlocationchanged at the moment)...

I have taken other debugging steps... the locationManager.getLastKnownLocation().getprovider() is gps... i hardcoded that into the locationmanager provider to test different things and that seems to be the problem most people had when i googled this (using network_service instead of gps when geo fix uses gps)...

every tutorial I have seen has been an intentservice or activity that implements locationListener, but I want the location listener peeled out separately... maybe that isn't possible or maybe just the emulator is having a problem?

I create a class and pass it the Context(Intent)

public class TestGPS{

    private Context context;
    private MyLocationListener locationListener;
    private LocationManager locationManager;

    public TestGPS(Context callingContext) { 
        this.context = callingContext;

        // Acquire a reference to the system Location Manager
        this.locationManager = (LocationManager) this.context.getSystemService(Context.LOCATION_SERVICE);

        // Define a listener that responds to location updates
        this.locationListener = new MyLocationListener(this.context, locationManager);
    }

this class requests new gps coordinates with

public void doStuff(){
    String provider = null;
    boolean gpsEnabledLocation = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    boolean networkEnabledLocation = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

    if (networkEnabledLocation)
    {
        provider = LocationManager.NETWORK_PROVIDER;
    }
    else if (gpsEnabledLocation)
    {
        provider = LocationManager.GPS_PROVIDER;
    }
        this.locationManager.requestLocationUpdates(provider, 1000, 0, this.locationListener);

    while (!this.locationListener.Triggered)
    {
        Location lastLocation = this.locationManager.getLastKnownLocation(provider);
        if (lastLocation != null) {
            String lastLocationString = "Latitude: " + lastLocation.getLatitude() + ". Longitude: " + lastLocation.getLongitude() + ". Provided by: " + lastLocation.getProvider() + " at " + lastLocation.getTime();
            Log.i("locationManager", lastLocationString);
        }

        Thread.sleep(5 * 1000);
    }

    this.locationManager.removeUpdates(this.locationListener);
}

then the class that implements locationlistener

public class MyLocationListener implements LocationListener {

    public boolean Triggered = false;

    @Override
    public void onLocationChanged(Location location) {
        Log.i("onLocationChanged", "Triggered");
        this.Triggered = true;
    }

    ... the other 3 functions
}

Thanks again!

  • Just hooked it up to my phone and got same results... it isn't an emulator problem the phone used network provider but getLastKnownLocation was update while onlocationchanged was never called – Logan Manning Feb 13 '15 at 18:36
  • See my answer here for that: https://stackoverflow.com/questions/28502513/how-get-the-current-latitud-and-longitud-without-use-getlastknownlocation-method/28502691#28502691 – Rensodarwin Feb 13 '15 at 19:55

0 Answers0