Search code examples
androidgpslocationlistener

Android GPS onLocationChanged never called


I have an android application and I would like to obtain the location.

For this I follow the android documentation, but it doesn't work. The onLocationChanged method is never called.

public class GPSTestActivity extends Activity
{   
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
    {
        Toast.makeText(this, "GPS available", 3000).show();
    }
    else
    {
        Toast.makeText(this, "GPS not available", 3000).show();
    }

    LocationListener locationListener = new LocationListener()
    {
        public void onLocationChanged(Location location)
        {
            System.out.println("RECEIVED LOCATION");
        }

        public void onStatusChanged(String provider, int status, Bundle extras)
        {
        }

        public void onProviderEnabled(String provider)
        {
        }

        public void onProviderDisabled(String provider)
        {
        }


    };

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
}

In the AndroidManifest.mf I've include the permision:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

I'm trying to test the application with the emulator in DDMS console. I set a longitude and a latitude, then presss the send button, but the onLocationChanged method is not called.

What I'm doing wrong?

Thank in advance


Solution

  • I've found the problem.

    I was testing with Android 2.3.3. and there is a bug. Testing with Google API (Level 10) works perfect.