Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.gabriel.myapplication;
- import android.app.Dialog;
- import android.content.Context;
- import android.location.Location;
- import android.os.Bundle;
- import android.util.Log;
- import com.google.android.gms.common.ConnectionResult;
- import com.google.android.gms.common.GooglePlayServicesClient;
- import com.google.android.gms.common.GooglePlayServicesUtil;
- import com.google.android.gms.location.LocationClient;
- public class User implements GooglePlayServicesClient.ConnectionCallbacks {
- private static final String TAG = User.class.getSimpleName();
- private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
- private final Context mContext;
- private LocationClient mLocationClient;
- private Location mCurrentLocation;
- public User(Context context) {
- mContext = context;
- int resultCode =
- GooglePlayServicesUtil.
- isGooglePlayServicesAvailable(context);
- // If Google Play services is available
- if (ConnectionResult.SUCCESS == resultCode) {
- // In debug mode, log the status
- Log.d(TAG,
- "Google Play services is available.");
- // Google Play services was not available for some reason.
- // resultCode holds the error code.
- } else {
- Log.d(TAG,
- "Google Play services isn't available.");
- // Get the error dialog from Google Play services
- // Dialog errorDialog = GooglePlayServicesUtil.getErrorDialog(
- // resultCode,
- // mContext,
- // CONNECTION_FAILURE_RESOLUTION_REQUEST);
- //
- // // If Google Play services can provide an error dialog
- // if (errorDialog != null) {
- // // Create a new DialogFragment for the error dialog
- // ErrorDialogFragment errorFragment =
- // new ErrorDialogFragment();
- // // Set the dialog in the DialogFragment
- // errorFragment.setDialog(errorDialog);
- // // Show the error dialog in the DialogFragment
- // errorFragment.show(getSupportFragmentManager(),
- // "Location Updates");
- // }
- }
- // todo: organizar esse código abaixo talvez?
- mLocationClient = new LocationClient(mContext, this, null);
- //O Correto em uma activity
- // @Override
- // protected void onStart() {
- // super.onStart();
- //
- // // Connect to LocationServices
- // mLocationClient.connect();
- // }
- //
- // @Override
- // protected void onStop() {
- //
- // // Stop updates
- // mLocationClient.removeLocationUpdates(this);
- //
- // // Disconnect from LocationServices
- // mLocationClient.disconnect();
- //
- // super.onStop();
- // }
- mLocationClient.connect();
- }
- public Location GetCurrentLocation() {
- Location bestResult = null;
- float bestAccuracy = Float.MAX_VALUE;
- long bestTime = Long.MIN_VALUE;
- if (mCurrentLocation != null) {
- float accuracy = mCurrentLocation.getAccuracy();
- long time = mCurrentLocation.getTime();
- if (accuracy < bestAccuracy) {
- bestResult = mCurrentLocation;
- bestAccuracy = accuracy;
- bestTime = time;
- }
- }
- // Return best reading or null
- if (bestAccuracy > 20 || (System.currentTimeMillis() - bestTime) > 20) {
- return null;
- } else {
- return bestResult;
- }
- }
- @Override
- public void onConnected(Bundle bundle) {
- Log.d(TAG, "++onConnected++");
- // Get the best most recent location currently available
- mCurrentLocation = mLocationClient.getLastLocation();
- }
- @Override
- public void onDisconnected() {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement