How does an app use the Location Services API within
an Activity?
To access a device’s location, apps register to get updates
from Google Play services. Then when done, apps unregister. The paradigm
of releasing resources
when an app is done with them is common for hardware
access, where the hardware in question consumes significant power or needs
to be explicitly stopped.
Using the Location Services API requires several steps
to register to receive updates, receive them, and then unregister when
the app doesn’t need them anymore. It has several important parts:
-
Specifies location requirements—During onCreate(), the app
creates a LocationRequest. It describes how frequently the app wants to
receive updates and how accurate they need to be.
-
Connects—During onStart() the Activity connects to Google
Play services. When Location Services is ready, Android calls onConnected().
-
Registers to receive updates: In startPeriodicUpdates(),
the app registers a LocationListener.
-
Receives location updates—Location Services passes Location
objects back to onLocationChanged().
-
Release resources—When the user leaves the app, it calls
onStop(), which stops further updates and disconnects. This step is important
because without it, the device would continue to collect locations forever.
An example of using Location Services.
Senses location
public class
LocationActivity extends
Activity implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener,
LocationListener {
private static final
String TAG =
"LocationActivity";
// A request to
connect to Location
Services
private
LocationRequest
mLocationRequest;
// Stores the current
instantiation of the
location client in this
object
private
LocationClient
mLocationClient;
private TextView
locationLog;
@Override
protected void
onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.locations
locationLog =
(TextView)
findViewById(R.id.tv_location_createLocationRequest();
}
private void
createLocationRequest() {
mLocationRequest
=
LocationRequest.create();
mLocationRequest.setInterval
mLocationRequest.setPriorit
mLocationRequest.setFastest
mLocationClient =
new LocationClient(this,
this, this);
}
// control connecting
and disconnecting during
visible time
// of the activity
@Override
public void onStop()
{
if
(mLocationClient.isConnected())
{
stopPeriodicUpdates();
}
mLocationClient.disconnect();
super.onStop();
}
@Override
public void onStart()
{
mLocationClient.connect();
super.onStart();
}
@Override
public void
onConnected(Bundle
bundle) {
final int
resultCode =
GooglePlayServicesUtil
.isGooglePlayServicesAvailable(// If Google Play
services is available
if
(ConnectionResult.SUCCESS
== resultCode) {
startPeriodicUpdates();
} else {
Log.e(TAG,
"cannot connect to
location services");
}
}
private void
startPeriodicUpdates() {
mLocationClient.requestLocationUpdates(this);
}
public void
stopPeriodicUpdates() {
mLocationClient.removeLocationUpdates(}
@Override
public void
onDisconnected() {
Log.e(TAG,
"disconnected");
}
@Override
public void
onConnectionFailed(ConnectionResult
connectionResult) {
Log.e(TAG,
"failed to connect");
}
@Override
public void
onLocationChanged(Location
loc) {
Log.d(TAG,
"LOCATION UPDATE");
final String
summary =
loc.getLatitude() + ", "
+ loc.getLongitude()
+ " " +
loc.getAccuracy();
locationLog.setText(summary);
}
} |