Advertisement
Guest User

Untitled

a guest
Aug 30th, 2014
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.00 KB | None | 0 0
  1. package com.example.gabriel.myapplication;
  2.  
  3. import android.app.Dialog;
  4. import android.content.Context;
  5. import android.location.Location;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8.  
  9. import com.google.android.gms.common.ConnectionResult;
  10. import com.google.android.gms.common.GooglePlayServicesClient;
  11. import com.google.android.gms.common.GooglePlayServicesUtil;
  12. import com.google.android.gms.location.LocationClient;
  13.  
  14. public class User implements GooglePlayServicesClient.ConnectionCallbacks {
  15.  
  16.     private static final String TAG = User.class.getSimpleName();
  17.     private final static int  CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
  18.     private final Context mContext;
  19.     private LocationClient mLocationClient;
  20.     private Location mCurrentLocation;
  21.  
  22.     public User(Context context) {
  23.         mContext = context;
  24.  
  25.         int resultCode =
  26.                 GooglePlayServicesUtil.
  27.                         isGooglePlayServicesAvailable(context);
  28.         // If Google Play services is available
  29.         if (ConnectionResult.SUCCESS == resultCode) {
  30.             // In debug mode, log the status
  31.             Log.d(TAG,
  32.                     "Google Play services is available.");
  33.             // Google Play services was not available for some reason.
  34.             // resultCode holds the error code.
  35.         } else {
  36.             Log.d(TAG,
  37.                     "Google Play services isn't available.");
  38.             // Get the error dialog from Google Play services
  39. //            Dialog errorDialog = GooglePlayServicesUtil.getErrorDialog(
  40. //                    resultCode,
  41. //                    mContext,
  42. //                    CONNECTION_FAILURE_RESOLUTION_REQUEST);
  43. //
  44. //            // If Google Play services can provide an error dialog
  45. //            if (errorDialog != null) {
  46. //                // Create a new DialogFragment for the error dialog
  47. //                ErrorDialogFragment errorFragment =
  48. //                        new ErrorDialogFragment();
  49. //                // Set the dialog in the DialogFragment
  50. //                errorFragment.setDialog(errorDialog);
  51. //                // Show the error dialog in the DialogFragment
  52. //                errorFragment.show(getSupportFragmentManager(),
  53. //                        "Location Updates");
  54. //            }
  55.         }
  56.  
  57. //        todo: organizar esse código abaixo talvez?
  58.         mLocationClient = new LocationClient(mContext, this, null);
  59.         //O Correto em uma activity
  60. //        @Override
  61. //        protected void onStart() {
  62. //            super.onStart();
  63. //
  64. //            // Connect to LocationServices
  65. //            mLocationClient.connect();
  66. //        }
  67. //
  68. //        @Override
  69. //        protected void onStop() {
  70. //
  71. //            // Stop updates
  72. //            mLocationClient.removeLocationUpdates(this);
  73. //
  74. //            // Disconnect from LocationServices
  75. //            mLocationClient.disconnect();
  76. //
  77. //            super.onStop();
  78. //        }
  79.         mLocationClient.connect();
  80.     }
  81.  
  82.     public Location GetCurrentLocation() {
  83.         Location bestResult = null;
  84.         float bestAccuracy = Float.MAX_VALUE;
  85.         long bestTime = Long.MIN_VALUE;
  86.  
  87.         if (mCurrentLocation != null) {
  88.  
  89.             float accuracy = mCurrentLocation.getAccuracy();
  90.             long time = mCurrentLocation.getTime();
  91.  
  92.             if (accuracy < bestAccuracy) {
  93.  
  94.                 bestResult = mCurrentLocation;
  95.                 bestAccuracy = accuracy;
  96.                 bestTime = time;
  97.  
  98.             }
  99.         }
  100.  
  101.         // Return best reading or null
  102.         if (bestAccuracy > 20 || (System.currentTimeMillis() - bestTime) > 20) {
  103.             return null;
  104.         } else {
  105.             return bestResult;
  106.         }
  107.     }
  108.  
  109.     @Override
  110.     public void onConnected(Bundle bundle) {
  111.         Log.d(TAG, "++onConnected++");
  112.         // Get the best most recent location currently available
  113.         mCurrentLocation = mLocationClient.getLastLocation();
  114.     }
  115.  
  116.     @Override
  117.     public void onDisconnected() {
  118.  
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement