Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use GoogleApiClient instead of deprecated classes #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
import android.widget.TextView;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

public class LocationGetLocationActivity extends Activity implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener, LocationListener {
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, LocationListener {

private static final long ONE_MIN = 1000 * 60;
private static final long TWO_MIN = ONE_MIN * 2;
Expand Down Expand Up @@ -55,7 +55,7 @@ public class LocationGetLocationActivity extends Activity implements
private final String TAG = "LocationGetLocationActivity";

private boolean mFirstUpdate = true;
private LocationClient mLocationClient;
private GoogleApiClient mGoogleApiClient = null;
private Location mCurrentLocation;

@Override
Expand All @@ -73,7 +73,11 @@ public void onCreate(Bundle savedInstanceState) {
mLngView = (TextView) findViewById(R.id.lng_view);

// Create new Location Client. This class will handle callbacks
mLocationClient = new LocationClient(this, this, this);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();

// Create and define the LocationRequest
mLocationRequest = LocationRequest.create();
Expand All @@ -94,17 +98,13 @@ protected void onStart() {
super.onStart();

// Connect to LocationServices
mLocationClient.connect();
mGoogleApiClient.connect();
}

@Override
protected void onStop() {

// Stop updates
mLocationClient.removeLocationUpdates(this);

// Disconnect from LocationServices
mLocationClient.disconnect();
mGoogleApiClient.disconnect();

super.onStop();
}
Expand All @@ -130,7 +130,7 @@ public void onLocationChanged(Location location) {
updateDisplay(location);

if (mBestReading.getAccuracy() < MIN_ACCURACY)
mLocationClient.removeLocationUpdates(this);
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);

}
}
Expand Down Expand Up @@ -161,23 +161,29 @@ public void onConnected(Bundle dataBundle) {
|| mBestReading.getTime() < System.currentTimeMillis()
- TWO_MIN) {

mLocationClient.requestLocationUpdates(mLocationRequest, this);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

// Schedule a runnable to unregister location listeners
Executors.newScheduledThreadPool(1).schedule(new Runnable() {
// TODO: fix passing listener into FusedLocationApi.removeLocationUpdates
/*Executors.newScheduledThreadPool(1).schedule(new Runnable() {

@Override
public void run() {

mLocationClient.removeLocationUpdates(LocationGetLocationActivity.this);
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);

}
}, MEASURE_TIME, TimeUnit.MILLISECONDS);
}, MEASURE_TIME, TimeUnit.MILLISECONDS);*/
}

}
}

@Override
public void onConnectionSuspended(int cause) {
//TODO
}

// Get the last known location from all providers
// return best reading is as accurate as minAccuracy and
// was taken no longer then maxAge milliseconds ago
Expand All @@ -189,7 +195,7 @@ private Location bestLastKnownLocation(float minAccuracy, long maxAge) {
long bestTime = Long.MIN_VALUE;

// Get the best most recent location currently available
mCurrentLocation = mLocationClient.getLastLocation();
mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

if (mCurrentLocation != null) {

Expand All @@ -214,13 +220,6 @@ private Location bestLastKnownLocation(float minAccuracy, long maxAge) {
}


@Override
public void onDisconnected() {

Log.i(TAG, "Disconnected. Try again later.");

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.i(TAG, "Connection Failed. Try again later.");
Expand Down