Open main menu

CDOT Wiki β

Changes

MAP524/DPS924 Lecture 9

1,155 bytes added, 04:50, 5 August 2015
Getting your location
<source lang="java">Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);</source>
*Now you're ready to ask to receive location updates from the location manager. Easiest way to do that is via a callback, so we'll make our activity implement LocationListener and add this code to start/stop getting updates:<source lang="java">locationManager.requestLocationUpdates(provider, 400, 1, this);// ...locationManager.removeUpdates(this);</source>* The code above should live in onResume()/onPause() (remember [[MAP524/DPS924_Lecture_3#Activity|the activity lifecycle]]) because getting the location uses significant battery power and your app will continue to get location even if it's not in the foreground (e.g. if the lock screen is on).* The required LocationListener method onLocationChanged() will be called when the location is updated.* On an emulator you won't get any location updates automatically, you'll need to set them using telnet:<pre>$ telnet localhost 5554Trying 127.0.0.1...Connected to localhost.Escape character is '^]'.Android Console: type 'help' for a list of commandsOKgeo fix 13.24 52.31OK</pre>* Or the Emulator Control panel in Android Device Monitor: [[Image:emulator-control-panel.png|300px| ]]
== Setup to use Google Play Services ==