Take the 2-minute tour ×

I want to get the current location of the device using the gps provider, but this actually waits for the gps position to change.

So I have a simple application that listens for the location change. This works with NETWORK_PROVIDER instantly, but for GPS_PROVIDER I waited for 15 minutes and no action has occured.

I'm using API level 8.

public class MainActivity extends Activity {
    TextView tv;

    public void onCreate(Bundle savedInstanceState) {
        tv = (TextView) this.findViewById(R.id.textView1);
        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener ll = new mylocationlistener();

        //lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, ll); // this works fine
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);

    private class mylocationlistener implements LocationListener {
        public void onLocationChanged(Location location) {
            Date today = new Date();
            Timestamp currentTimeStamp = new Timestamp(today.getTime());
            if (location != null) {
                Log.d("LOCATION CHANGED", location.getLatitude() + "");
                Log.d("LOCATION CHANGED", location.getLongitude() + "");
                String str = "\n CurrentLocation: " + "\n Latitude: "
                        + location.getLatitude() + "\n Longitude: "
                        + location.getLongitude() + "\n Accuracy: "
                        + location.getAccuracy() + "\n CurrentTimeStamp "
                        + currentTimeStamp + "\n Altitude: "
                        + location.getAltitude() + "\n Bearing"
                        + location.getBearing() + "\n Speed"
                        + location.getSpeed() + "\n ";
                Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG)

        public void onProviderDisabled(String provider) {
            Toast.makeText(MainActivity.this, "Error onProviderDisabled",

        public void onProviderEnabled(String provider) {
            Toast.makeText(MainActivity.this, "onProviderEnabled",

            Toast.makeText(MainActivity.this, "onStatusChanged",

I did put the permission in the manifest, so this is not the problem.

share|improve this question
Are you sure you have your GPS enabled? –  gsingh2011 Dec 10 '12 at 16:03
Yes, I am very sure... –  Chirila Alexandru Dec 10 '12 at 16:03
This depends on your device and also the GPS signal strength, because newer mobiles have high sensitive GPS system but old phone requires open sky to detect signals. –  Ali Imran Dec 10 '12 at 16:28
add comment

1 Answer

up vote 2 down vote accepted

Are you testing inside? Unless you are outside, you may not get a GPS lock at all, which will mean that the phone will attempt to get a lock as often as it can (as you have specified 0 and 0 for the minimum time and distance in your requestLocationUpdates call).

Unfortunately, you just have to wait for the device to get a GPS lock, and if it doesn't because of a lack of signal, you'll just keep waiting. The first thing you can do to alleviate this is to try to get the last known location for the GPS provider. This will return a location if the device recently had a GPS lock and stored a location:

Location lastLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

Other than that, you should just try to get the location from the network provider, since that is likely to return a location much quicker than GPS, as you have already observed. This is because of the abundance of sources of location data (cell towers and WIFI), that are available everywhere, including indoors, as long as you have a wireless signal.

share|improve this answer
Yeah, I'm inside, I didn't realize that being inside will make me lose all the signal... guess i will just have to use the emulator... –  Chirila Alexandru Dec 10 '12 at 17:09
That's a good idea. There's also an excellent app that someone else out there made called "Fake GPS" that lets you simulate a location lock for testing purposes. I highly suggest you try that if you just need to debug a location based app. Here's the link: play.google.com/store/apps/details?id=com.lexa.fakegps –  mattgmg1990 Dec 10 '12 at 19:44
add comment

Your Answer


