1

I am trying to set a GPS position on my emulator with telnet and using DDMS perspective. Nothing seems to be working, because if you think my code the output on the screen will be "Couldn't get a GPS location". The application works when I run it on a phone. Code:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    tvLat = (TextView) findViewById(R.id.tvLat);
    tvLongi = (TextView) findViewById(R.id.tvLongi);
    tvLat.setText("test");
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria crit = new Criteria();
    providers = lm.getBestProvider(crit, false);
    Location loc = lm.getLastKnownLocation(providers);
    if (loc != null) {
        x =  loc.getLatitude();
        y =  loc.getLongitude();

        t = "Current latitude: " + x;
        t1 = "Current longitude: " + y;
        tvLat.setText(t);
        tvLongi.setText(t1);
    } else {
        tvLat.setText("Couldn't get a GPS location");
    }

}

@Override
public void onLocationChanged(Location loc) {
    // TODO Auto-generated method stub
    x = loc.getLatitude();
    y = loc.getLongitude();
    t = "Current latitude: " + x;
    t1 = "Current longitude: " + y;

    tvLat.setText(t);
    tvLongi.setText(t1);

}

@Override
public void onProviderDisabled(String arg0) {
    // TODO Auto-generated method stub
    tvLat.setText("GPS disabled");
}

@Override
public void onProviderEnabled(String arg0) {

    tvLat.setText("GPS enabled");
}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {


}
Permissions:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"></uses-permission>
Carlj901
  • 1,329
  • 4
  • 24
  • 39
  • possible duplicate of [How to emulate GPS location in the Android Emulator?](http://stackoverflow.com/questions/2279647/how-to-emulate-gps-location-in-the-android-emulator) – Jim G. Jun 18 '13 at 14:53

1 Answers1

3

From the DDMS Perspective from the Emulator Control tab located in the bottom-left of Eclipse you have Locations Controls. From there you can input the longitude and latitude and click Send to send those coordinates to the selected emulator.

Maybe you will still not be able to get the last know location, but this will fire the onLocationChanged() method.

EDIT: I see that your class implements LocationListener. Did you register your class for the location changes using lm.requestLocationUpdates() ?

EDIT2: I mean :

lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,this);
lm.requestLocationUpdates(LocationManager.GPS, 0, 0,this);
Ovidiu Latcu
  • 71,607
  • 15
  • 76
  • 84
  • As I said, I already tried with DDMS perspective but I dont get any different output on my screen. – Carlj901 Nov 29 '11 at 12:27
  • What do you mean with "register your class"? – Carlj901 Nov 29 '11 at 12:33
  • If i add: lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,this); and lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,this); The output will be "GPS disabled" on the screen and if I try to set a position using DDMS it says "Unable to send command to emulator" – Carlj901 Nov 29 '11 at 12:45