Carlj901
Carlj901

Reputation: 1411

Get GPS location when application is started in Emulator

I am testing my application in Android Emulator and I am only able to set a GPS position when the application is already running through the onLocationChanged method. I can't set it before I start the application. This is the code in my onCreate method:

Edit: Think you guys missunderstood me. I am able to set a GPS position with geo fix when application is running. When I try to set it before I launch the activity I get Error android.view.windowmanager$badtokenexception.

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

Criteria crit = new Criteria();
providers = lm.getBestProvider(crit, false);
loc = lm.getLastKnownLocation(providers);
    if (loc != null) {
    title.setText(stop_name);
    lview.setAdapter(lviewAdapter);
      } else {
        Toast t = Toast.makeText(this, "Failed to get GPS location", 5000);
        t.setGravity(Gravity.BOTTOM, 0, 0);
        t.show();
        }

Upvotes: 1

Views: 276

Answers (3)

Prasham
Prasham

Reputation: 6686

You even don't need to open command line tool, if you are using eclipse [I hope you do], if not you don't have to reed the rest....

  1. Go to window-> Show view-> Other
  2. In a dialog box you can find android group[folder]
  3. Click on emulator control
  4. Adjust the window as per your convenience
  5. If you have more than one simulators/devices connected repeat 1 and 2
  6. Here you have to click devices
  7. In the devices click on desired simulator
  8. Again go to emulator control window just opened
  9. Find Location Controls [Keep in mind if selected device is not emulator all the controls in emulator controls are disabled]
  10. Type desired location as per your convenient format.
  11. Press send And Viola.... you can easily work with locations in emulator

Upvotes: 1

Abhinav Singh Maurya
Abhinav Singh Maurya

Reputation: 3313

enter image description here

As you can see in the image you can send lat/long manually from your eclipse by going to DDMS an add lat/long values manually and send it you can get these points in your emulator by doing this

Upvotes: 1

Maneesh
Maneesh

Reputation: 6128

to set the gps location in emulator you have to follow below steps and open your app before setting up below steps ,then this will go to last configuration .

  1. Open command prompt
  2. Type "telnet localhost 5554"
  3. then type geo fix

enter image description here

Upvotes: 1

Related Questions