Last active
October 17, 2015 19:16
-
-
Save morganwilde/f47146e1ea9cb61047da to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package co.developertime.android.recon; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.location.Criteria; | |
import android.location.Location; | |
import android.location.LocationManager; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.TextView; | |
import com.google.android.gms.common.ConnectionResult; | |
import com.google.android.gms.common.api.GoogleApiClient; | |
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks; | |
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener; | |
import com.google.android.gms.location.LocationListener; | |
import com.google.android.gms.location.LocationRequest; | |
import com.google.android.gms.location.LocationServices; | |
public class MainActivity extends Activity | |
implements ConnectionCallbacks, | |
OnConnectionFailedListener, | |
LocationListener { | |
private final static String TAG = "MainActivity"; | |
public final static String EXTRA_MESSAGE = "co.developertime.android.MESSAGE"; | |
// Views | |
private TextView mMessageTextView; | |
private TextView mLocationLongtitudeTextView; | |
private TextView mLocationLatitudeTextView; | |
private LocationRequest mLocationRequest; | |
private GoogleApiClient mGoogleApiClient; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
// Fetch references to views | |
mMessageTextView = (TextView) findViewById(R.id.edit_message); | |
mLocationLongtitudeTextView = (TextView) findViewById(R.id.location_longtitude_text_view); | |
mLocationLatitudeTextView = (TextView) findViewById(R.id.location_latitude_text_view); | |
Log.i(TAG, "onCreate()"); | |
mGoogleApiClient = new GoogleApiClient.Builder(this) | |
.addApi(LocationServices.API) | |
.addConnectionCallbacks(this) | |
.addOnConnectionFailedListener(this) | |
.build(); | |
if (mGoogleApiClient != null) { | |
mGoogleApiClient.connect(); | |
} | |
} | |
@Override | |
protected void onStart() { | |
super.onStart(); | |
} | |
@Override | |
protected void onPause() { | |
super.onPause(); | |
} | |
@Override | |
protected void onStop() { | |
super.onStop(); | |
if (mGoogleApiClient.isConnected()) { | |
mGoogleApiClient.disconnect(); | |
} | |
} | |
// View callbacks | |
public void sendMessage(View view) { | |
Log.i(TAG, "sendMessage"); | |
Intent intent = new Intent(this, DisplayMessageActivity.class); | |
String message = mMessageTextView.getText().toString(); | |
intent.putExtra(EXTRA_MESSAGE, message); | |
startActivity(intent); | |
} | |
@Override | |
public void onConnected(Bundle bundle) { | |
Log.i(TAG, "onConnected()"); | |
Location lastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); | |
Log.i(TAG, "mGoogleApiClient.isConnected(): " + mGoogleApiClient.isConnected()); | |
Log.i(TAG, "lastLocation: " + lastLocation); | |
if (lastLocation != null) { | |
String latitude = String.valueOf(lastLocation.getLatitude()); | |
String longtitude = String.valueOf(lastLocation.getLongitude()); | |
mLocationLongtitudeTextView.setText(longtitude); | |
mLocationLatitudeTextView.setText(latitude); | |
} else { | |
createLocationRequest(); | |
startLocationUpdates(); | |
} | |
} | |
protected void createLocationRequest() { | |
mLocationRequest = new LocationRequest(); | |
mLocationRequest.setInterval(10000); | |
mLocationRequest.setFastestInterval(5000); | |
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); | |
} | |
protected void startLocationUpdates() { | |
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); | |
} | |
@Override | |
public void onConnectionSuspended(int i) { | |
Log.i(TAG, "onConnectionSuspended()"); | |
} | |
@Override | |
public void onConnectionFailed(ConnectionResult connectionResult) { | |
Log.i(TAG, "onConnectionFailed()"); | |
} | |
@Override | |
public void onLocationChanged(Location location) { | |
Log.i(TAG, "onLocationChanged() " + location.getLatitude()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment