I am wanting to implement location based functionality in my app. I have been reading a bit and I find my self a bit confused.
When googling for tutorial almost every result comes back with an example using Android Location API.
However when reading the android developer guidelines they state the following:
The Google Play services location APIs are preferred over the Android framework location APIs (android.location) as a way of adding location awareness to your app. If you are currently using the Android framework location APIs, you are strongly encouraged to switch to the Google Play services location APIs as soon as possible.
Android Docs
So this is telling me not to go for the simpler route of just implementing a Location Listener.
So my question is, what is the difference between the two? Why would I use one and not the other?
Where can I find a decent tutorial on how to safely and accurately access the Google play services location API properly.
I have tried this so far (as suggested on the Android site), However none of my Call Backs are being called.
public class LocationManager implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private Context mContext;
private GoogleApiClient mGoogleApiClient;
private Location mLastLocation;
public LocationManager(Context context) {
mContext = context;
//
if (checkIfGooglePlayServicesAreAvailable()) {
//Get Access to the google service api
buildGoogleApiClient();
} else {
//Use Android Location Services
//TODO:
}
}
public Location getCoarseLocation() {
if (mLastLocation != null) {
return mLastLocation;
} else return null;
}
private synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(mContext)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
private boolean checkIfGooglePlayServicesAreAvailable() {
int errorCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(mContext);
if (errorCode != ConnectionResult.SUCCESS) {
GooglePlayServicesUtil.getErrorDialog(errorCode, (MainActivity) mContext, 0).show();
return false;
}
return true;
}
@Override
public void onConnected(Bundle bundle) {
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location != null) {
mLastLocation = location;
Toast.makeText(mContext, location.getLongitude() + " , " + location.getLatitude() + " : " + location.getAccuracy(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onConnectionSuspended(int i) {
Toast.makeText(mContext, "suspended", Toast.LENGTH_LONG).show();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Toast.makeText(mContext, connectionResult.toString(), Toast.LENGTH_LONG).show();
}
}
I then call my LocationManager From My Activity:
LocationManager locationManager = new LocationManager(this);
Location location = locationManager.getCoarseLocation();
//Use Location
I want to make a helper class which I can simply call from any activity or fragment. However when I run the following, the constructor executes successfully. However none of my breakpoints in the callbacks get hit. even after 1 or 2 minutes.