Advertisement
joris

Code

Sep 14th, 2015
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.69 KB | None | 0 0
  1. import android.app.Activity;
  2. import android.location.Location;
  3. import android.os.Bundle;
  4. import android.support.v4.app.Fragment;
  5. import android.util.Log;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.widget.TextView;
  10. import android.widget.Toast;
  11.  
  12. import info.androidhive.materialdesign.R;
  13.  
  14. import com.google.android.gms.common.ConnectionResult;
  15. import com.google.android.gms.common.GooglePlayServicesUtil;
  16. import com.google.android.gms.common.api.GoogleApiClient;
  17. import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
  18. import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
  19. import com.google.android.gms.location.LocationRequest;
  20. import com.google.android.gms.location.LocationServices;
  21.  
  22.  
  23. public class GpsFragment extends Fragment implements ConnectionCallbacks,
  24.         OnConnectionFailedListener {
  25.  
  26.     private static String TAG = MainActivity.class.getSimpleName();
  27.  
  28.     public GpsFragment() {
  29.         // Required empty public constructor
  30.     }
  31.  
  32.     private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 1000;
  33.     private Location mLastLocation;
  34.  
  35.     // Google client to interact with Google API
  36.     private GoogleApiClient mGoogleApiClient;
  37.  
  38.     // boolean flag to toggle periodic location updates
  39.     private boolean mRequestingLocationUpdates = false;
  40.  
  41.     private LocationRequest mLocationRequest;
  42.  
  43.     public TextView lblLocation;
  44.  
  45.     // Location updates intervals in sec
  46.     private static int UPDATE_INTERVAL = 10000; // 10 sec
  47.     private static int FATEST_INTERVAL = 5000; // 5 sec
  48.     private static int DISPLACEMENT = 10; // 10 meters
  49.  
  50.     @Override
  51.     public void onCreate(Bundle savedInstanceState) {
  52.         super.onCreate(savedInstanceState);
  53.     }
  54.  
  55.     @Override
  56.     public View onCreateView(LayoutInflater inflater, ViewGroup container,
  57.                              Bundle savedInstanceState) {
  58.         View rootView = inflater.inflate(R.layout.fragment_gps, container, false);
  59.  
  60.         lblLocation = (TextView)rootView.findViewById(R.id.lblLocation);
  61.  
  62.         // First we need to check availability of play services
  63.         if (checkPlayServices()) {
  64.             buildGoogleApiClient();
  65.         }
  66.  
  67.         return rootView;
  68.     }
  69.  
  70.     private void displayLocation() {
  71.  
  72.         mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
  73.  
  74.         if (mLastLocation != null) {
  75.             double latitude = mLastLocation.getLatitude();
  76.             double longitude = mLastLocation.getLongitude();
  77.             lblLocation.setText(latitude + ", " + longitude);
  78.         } else {
  79.             //lblLocation.setText("(Couldn't get the location. Make sure location is enabled on the device)");
  80.         }
  81.     }
  82.  
  83.     protected synchronized void buildGoogleApiClient() {
  84.         mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
  85.                 .addConnectionCallbacks(this)
  86.                 .addOnConnectionFailedListener(this)
  87.                 .addApi(LocationServices.API).build();
  88.     }
  89.  
  90.     private boolean checkPlayServices() {
  91.         int resultCode = GooglePlayServicesUtil
  92.                 .isGooglePlayServicesAvailable(getActivity());
  93.         if (resultCode != ConnectionResult.SUCCESS) {
  94.             if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
  95.                 GooglePlayServicesUtil.getErrorDialog(resultCode, getActivity(),
  96.                         PLAY_SERVICES_RESOLUTION_REQUEST).show();
  97.             } else {
  98.                 Toast.makeText(getActivity().getApplicationContext(),
  99.                         "This device is not supported.", Toast.LENGTH_LONG)
  100.                         .show();
  101.                 getActivity().finish();
  102.             }
  103.             return false;
  104.         }
  105.         return true;
  106.     }
  107.  
  108.  
  109.  
  110.     @Override
  111.     public void onStart() {
  112.         super.onStart();
  113.         if (mGoogleApiClient != null) {
  114.             mGoogleApiClient.connect();
  115.         }
  116.     }
  117.  
  118.     @Override
  119.     public void onResume() {
  120.         super.onResume();
  121.         checkPlayServices();
  122.     }
  123.  
  124.     @Override
  125.     public void onConnectionFailed(ConnectionResult result) {
  126.         Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = "
  127.                 + result.getErrorCode());
  128.     }
  129.  
  130.     @Override
  131.     public void onConnected(Bundle arg0) {
  132.         displayLocation();
  133.     }
  134.  
  135.     @Override
  136.     public void onConnectionSuspended(int arg0) {
  137.         mGoogleApiClient.connect();
  138.     }
  139.  
  140.     @Override
  141.     public void onAttach(Activity activity) {
  142.         super.onAttach(activity);
  143.     }
  144.  
  145.     @Override
  146.     public void onDetach() {
  147.         super.onDetach();
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement