Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.app.Activity;
- import android.location.Location;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.TextView;
- import android.widget.Toast;
- import info.androidhive.materialdesign.R;
- import com.google.android.gms.common.ConnectionResult;
- import com.google.android.gms.common.GooglePlayServicesUtil;
- 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.LocationRequest;
- import com.google.android.gms.location.LocationServices;
- public class GpsFragment extends Fragment implements ConnectionCallbacks,
- OnConnectionFailedListener {
- private static String TAG = MainActivity.class.getSimpleName();
- public GpsFragment() {
- // Required empty public constructor
- }
- private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 1000;
- private Location mLastLocation;
- // Google client to interact with Google API
- private GoogleApiClient mGoogleApiClient;
- // boolean flag to toggle periodic location updates
- private boolean mRequestingLocationUpdates = false;
- private LocationRequest mLocationRequest;
- public TextView lblLocation;
- // Location updates intervals in sec
- private static int UPDATE_INTERVAL = 10000; // 10 sec
- private static int FATEST_INTERVAL = 5000; // 5 sec
- private static int DISPLACEMENT = 10; // 10 meters
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View rootView = inflater.inflate(R.layout.fragment_gps, container, false);
- lblLocation = (TextView)rootView.findViewById(R.id.lblLocation);
- // First we need to check availability of play services
- if (checkPlayServices()) {
- buildGoogleApiClient();
- }
- return rootView;
- }
- private void displayLocation() {
- mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
- if (mLastLocation != null) {
- double latitude = mLastLocation.getLatitude();
- double longitude = mLastLocation.getLongitude();
- lblLocation.setText(latitude + ", " + longitude);
- } else {
- //lblLocation.setText("(Couldn't get the location. Make sure location is enabled on the device)");
- }
- }
- protected synchronized void buildGoogleApiClient() {
- mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
- .addConnectionCallbacks(this)
- .addOnConnectionFailedListener(this)
- .addApi(LocationServices.API).build();
- }
- private boolean checkPlayServices() {
- int resultCode = GooglePlayServicesUtil
- .isGooglePlayServicesAvailable(getActivity());
- if (resultCode != ConnectionResult.SUCCESS) {
- if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
- GooglePlayServicesUtil.getErrorDialog(resultCode, getActivity(),
- PLAY_SERVICES_RESOLUTION_REQUEST).show();
- } else {
- Toast.makeText(getActivity().getApplicationContext(),
- "This device is not supported.", Toast.LENGTH_LONG)
- .show();
- getActivity().finish();
- }
- return false;
- }
- return true;
- }
- @Override
- public void onStart() {
- super.onStart();
- if (mGoogleApiClient != null) {
- mGoogleApiClient.connect();
- }
- }
- @Override
- public void onResume() {
- super.onResume();
- checkPlayServices();
- }
- @Override
- public void onConnectionFailed(ConnectionResult result) {
- Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = "
- + result.getErrorCode());
- }
- @Override
- public void onConnected(Bundle arg0) {
- displayLocation();
- }
- @Override
- public void onConnectionSuspended(int arg0) {
- mGoogleApiClient.connect();
- }
- @Override
- public void onAttach(Activity activity) {
- super.onAttach(activity);
- }
- @Override
- public void onDetach() {
- super.onDetach();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement