8

i'm tring to use the gps on the android emulator, i've the following code:

public class NL extends Activity {

 private LocationManager locmgr = null;

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nl);

        locmgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        Criteria crit = new Criteria();
        crit.setAccuracy(Criteria.ACCURACY_FINE);
        String provider = locmgr.getBestProvider(crit, true);
        Location loc = locmgr.getLastKnownLocation(provider);


        Toast msg = Toast.makeText(this, "Lon: " + Double.toString(loc.getLongitude()) + " Lat: " + Double.toString(loc.getLatitude()), Toast.LENGTH_SHORT);
        msg.show();
    }
}

i've added the following line at the manifest:

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

and i've set the gps location with the DDMS method and either with the geo fix method, but when i run the code, i get a NullPointerExeption at the Toast line, probably cause loc is null.

I don't understand where the error is... can you help me please?


UPDATE!

Thanks for your help, now i use the following code and i don't get any error, but it doesn't run the code inside onChangeLocation... it doesn't run the Toast and don't return any message in the log!

 public class NL extends Activity {

        private LocationManager locmgr = null;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.nl);

            locmgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

         // Define a listener that responds to location updates
            LocationListener locationListener = new LocationListener() {
                public void onLocationChanged(Location loc) {
                  // Called when a new location is found by the network location provider.
                    Log.i("NOTIFICATION","onLocationChenged Triggered");

                    Toast msg = Toast.makeText(NetworkLocator.this, "Lon: " + Double.toString(loc.getLongitude()) + " Lat: " + Double.toString(loc.getLatitude()), Toast.LENGTH_SHORT);
                    msg.show();
                }

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

                public void onProviderEnabled(String provider) {}

                public void onProviderDisabled(String provider) {}
              };

            // Register the listener with the Location Manager to receive location updates
            locmgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
        }
    }

Thanks!

Marco Faion
  • 607
  • 3
  • 14
  • 23
  • 2
    did you verify if *locmgr.getBestProvider* actually returns "gps"? anyway, you should definitely implement a *LocationListener* and pass it to LocationManager with *requestLocationUpdates()* – guido Jan 20 '11 at 10:08
  • the getBestProvider return "gps". have i to add a locationlistener even if i try to get the locatio with lastKnownLocation? thanks for your help – Marco Faion Jan 20 '11 at 10:14
  • @Marco Faion Please keep ur answers updated... I answered already once to you and you didn't give any feedback on the answer! > `http://stackoverflow.com/questions/4591765/android-sqlite-database-method-undefined-fot-type/4591843#4591843` – Beasly Jan 20 '11 at 10:22
  • hello Macro did u got the solution or not? – Pinki Jan 20 '11 at 10:32
  • thanks for your help, qith requestLocationUpdates the application run fine, but i think that the "onLocationChange is never triggered... not sure if this is because i've set to 0 the time and distance parameters: locmgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); – Marco Faion Jan 20 '11 at 11:35
  • 1
    put a Log output to the `onLocationChange` and look in Logcat if something get printed... – Beasly Jan 20 '11 at 13:16
  • i've followed your suggestion, the onLocationChanged never trigger! – Marco Faion Jan 20 '11 at 14:22
  • FOUND THE SOLUTION: LocationManager.NETWORK_PROVIDER is WRONG. correction: LocationManager.GPS_PROVIDER – Marco Faion Jan 20 '11 at 16:16

4 Answers4

9

Emulator just doesn't have any location at the beginning. According to the doc, 'getLastKnownLocation' method can return null, so it is ok. In that case you should wait for location updates (you can user requestLocationUpdates method from LocationManager). You can trigger location update on emulator's gps module by following command:

adb -e emu geo fix 50 50
Damian Kołakowski
  • 2,731
  • 22
  • 25
7

FOUND THE SOLUTION:

LocationManager.NETWORK_PROVIDER is WRONG.

correction: LocationManager.GPS_PROVIDER

Marco Faion
  • 607
  • 3
  • 14
  • 23
  • 1
    In my experience the emulator does not appear to respond to location changes sent via DDMS when the provider is set to NETWORK_PROVIDER, but will when it is set to GPS_PROVIDER as you have noted. This looks like a limitation in the emulator to me, or at least a behaviour that it not obvious. – Mick May 29 '13 at 23:01
1

if all you what you described is done than maybe you are not probably not ur gps is on in emulator.go to setting:->Location and Security:->and use gps satelites should be checked


edited:ithink you have to use location manager without criteria type.



LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

loc=mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

------than try to get long. and lat.

chikka.anddev
  • 9,569
  • 7
  • 38
  • 46
0

Are you sure that your emulated device is actually supporting GPS? I think there was an extra option for this...

This may help too:

Thanks! To all: You can change the Build Target for your project any time in Eclipse: Right-click the project in Package Explorer, select Properties > Android and then check 'Google API' for Project Target. -- Developers working with non-English culture settings might notice that pressing the Send button in Location Controls does not send a new location to the Android emulator. It's fixed with the upcoming release 7 of the SDK tools; for a quick fix you can change your locale to 'en'. (See code.google.com/p/android/issues/detail?id=915 for details.) – Patrick de Kleijn

source: GPS on emulator doesn't get the geo fix - Android

Community
  • 1
  • 1
Beasly
  • 1,517
  • 4
  • 20
  • 30