Android Open Source - myAndroidApps G P S Tracker






From Project

Back to project page myAndroidApps.

License

The source code is released under:

This is my first GitHub try. Be gentle :)

If you think the Android project myAndroidApps listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
 * @author Gidy Basin/*  w  ww.  j av a  2s. co  m*/
 */

package gidy.carpark.utils;

import gidy.carpark.ParkingLocation;
import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class GPSTracker extends Service implements LocationListener {

  private final Context mContext;

  // flag for GPS status
  boolean isGPSEnabled = false;

  // flag for network status
  boolean isNetworkEnabled = false;

  // flag for GPS status
  boolean canGetLocation = false;

  Location location; // location
  double latitude; // latitude
  double longitude; // longitude

  // The minimum distance to change Updates in meters
  private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

  // The minimum time between updates in milliseconds
  private static final long MIN_TIME_BW_UPDATES = 1000; // 1 second

  // Declaring a Location Manager
  protected LocationManager locationManager;

  public GPSTracker(Context context) {
    this.mContext = context;
    getLocation();
  }

  public Location getLocation() {
    try {
      locationManager = (LocationManager) mContext
          .getSystemService(LOCATION_SERVICE);

      // getting GPS status
      isGPSEnabled = locationManager
          .isProviderEnabled(LocationManager.GPS_PROVIDER);

      // getting network status
      isNetworkEnabled = locationManager
          .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

      if (!isGPSEnabled && !isNetworkEnabled) {
        // no network provider is enabled
        return null;
      } else {
        this.canGetLocation = true;
        // First get location from Network Provider
        if (isNetworkEnabled) {
          locationManager.requestLocationUpdates(
              LocationManager.NETWORK_PROVIDER,
              MIN_TIME_BW_UPDATES,
              MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
          Log.d("Network", "Network");
          if (locationManager != null) {
            location = locationManager
                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            if (location != null) {
              latitude = location.getLatitude();
              longitude = location.getLongitude();
            }
          }
        }
        // if GPS Enabled get lat/long using GPS Services
        else if (isGPSEnabled) {
          if (location == null) {
            locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER,
                MIN_TIME_BW_UPDATES,
                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
            Log.d("GPS Enabled", "GPS Enabled");
            if (locationManager != null) {
              location = locationManager
                  .getLastKnownLocation(LocationManager.GPS_PROVIDER);
              if (location != null) {
                latitude = location.getLatitude();
                longitude = location.getLongitude();
              }
            }
          }
        }
      }

    } catch (Exception e) {
      e.printStackTrace();
    }

    return location;
  }

  /**
   * Stop using GPS listener
   * Calling this function will stop using GPS in your app
   * */
  public void stopUsingGPS(){
    if(locationManager != null){
      locationManager.removeUpdates(GPSTracker.this);
    }       
  }

  /**
   * Function to get latitude
   * */
  public double getLatitude(){
    if(location != null){
      latitude = location.getLatitude();
    }

    // return latitude
    return latitude;
  }

  /**
   * Function to get longitude
   * */
  public double getLongitude(){
    if(location != null){
      longitude = location.getLongitude();
    }

    // return longitude
    return longitude;
  }

  /**
   * Function to check GPS/wifi enabled
   * @return boolean
   * */
  public boolean canGetLocation() {
    return this.canGetLocation;
  }

  /**
   * Function to show settings alert dialog
   * On pressing Settings button will lauch Settings Options
   * */
  public void showSettingsAlert(){
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

    // Setting Dialog Title
    alertDialog.setTitle("?????? GPS");

    alertDialog.setCancelable(false);

    // Setting Dialog Message
    alertDialog.setMessage("?????? ????? ???? ???????. ?????? ????????? ?? ?????.");

    // On pressing OK button
    alertDialog.setPositiveButton("?????", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
        ParkingLocation.isOkPressed = true;
      }
    });

    alertDialog.create();

    // Showing Alert Message
    alertDialog.show();
  }

  @Override
  public void onLocationChanged(Location location) {
  }

  @Override
  public void onProviderDisabled(String provider) {
  }

  @Override
  public void onProviderEnabled(String provider) {
  }

  @Override
  public void onStatusChanged(String provider, int status, Bundle extras) {
  }

  @Override
  public IBinder onBind(Intent arg0) {
    return null;
  }

}




Java Source Code List

gidy.carpark.HomeScreen.java
gidy.carpark.JustParkedActivity.java
gidy.carpark.ParkingLocation.java
gidy.carpark.WhereParkedActivity.java
gidy.carpark.utils.CommonUtils.java
gidy.carpark.utils.FileUtils.java
gidy.carpark.utils.GPSTracker.java
gidy.carpark.utils.LocationUtils.java
gidy.carpark.utils.LocationsAdapter.java
gidy.medappg.CallAmbulanceActivity.java
gidy.medappg.HomeMenu.java
gidy.medappg.MedAppLogger.java
gidy.medappg.activities.AllergicReactionActivity.java
gidy.medappg.activities.AsthmaActivity.java
gidy.medappg.activities.BleedingActivity.java
gidy.medappg.activities.BrokenBoneActivity.java
gidy.medappg.activities.BurnActivity.java
gidy.medappg.activities.ChokeActivity.java
gidy.medappg.activities.DiabeticActivity.java
gidy.medappg.activities.DistressActivity.java
gidy.medappg.activities.EpilepsyActivity.java
gidy.medappg.activities.HeadInjuryActivity.java
gidy.medappg.activities.HeartAttackActivity.java
gidy.medappg.activities.HypothermiaActivity.java
gidy.medappg.activities.MedicalIssueActivity.java
gidy.medappg.activities.MeningitisActivity.java
gidy.medappg.activities.PoisoningActivity.java
gidy.medappg.activities.StrokeActivity.java
gidy.medappg.activities.UnconsciousBreatingActivity.java
gidy.medappg.activities.UnconsciousNotBreathingActivity.java
gidy.medappg.videoActivities.AsthmaVideoActivity.java
gidy.medappg.videoActivities.BleedingVideoActivity.java
gidy.medappg.videoActivities.BrokenBoneVideoActivity.java
gidy.medappg.videoActivities.BurnVideoActivity.java
gidy.medappg.videoActivities.ChokeActivityVideo.java
gidy.medappg.videoActivities.DiabeticVideoActivity.java
gidy.medappg.videoActivities.DistressVideoActivity.java
gidy.medappg.videoActivities.EpilepsyVideoActivity.java
gidy.medappg.videoActivities.HeadInjuryVideoActivity.java
gidy.medappg.videoActivities.HeartAttackVideoActivity.java
gidy.medappg.videoActivities.PoisoningVideoActivity.java
gidy.medappg.videoActivities.StrokeVideoActivity.java
gidy.medappg.videoActivities.UnconsciousBreatingVideoActivity.java
gidy.medappg.videoActivities.UnconsciousNotBreathingVideoActivity.java
gidy.medappg.videoActivities.VideoActivity.java
gidy.medappnem.CallAmbulanceActivity.java
gidy.medappnem.HomeMenu2Activity.java
gidy.medappnem.HomeMenu3Activity.java
gidy.medappnem.HomeMenu4Activity.java
gidy.medappnem.HomeMenu5Activity.java
gidy.medappnem.HomeMenu.java
gidy.medappnem.MedAppLogger.java
gidy.medappnem.activities.AllergicReactionActivity.java
gidy.medappnem.activities.AsthmaActivity.java
gidy.medappnem.activities.BleedingActivity.java
gidy.medappnem.activities.BrokenBoneActivity.java
gidy.medappnem.activities.BurnActivity.java
gidy.medappnem.activities.ChokeActivity.java
gidy.medappnem.activities.DiabeticActivity.java
gidy.medappnem.activities.DistressActivity.java
gidy.medappnem.activities.EpilepsyActivity.java
gidy.medappnem.activities.HeadInjuryActivity.java
gidy.medappnem.activities.HeartAttackActivity.java
gidy.medappnem.activities.HypothermiaActivity.java
gidy.medappnem.activities.MedicalIssueActivity.java
gidy.medappnem.activities.MeningitisActivity.java
gidy.medappnem.activities.PoisoningActivity.java
gidy.medappnem.activities.StrokeActivity.java
gidy.medappnem.activities.UnconsciousBreatingActivity.java
gidy.medappnem.activities.UnconsciousNotBreathingActivity.java
gidy.medappnem.videoActivities.AsthmaVideoActivity.java
gidy.medappnem.videoActivities.BleedingVideoActivity.java
gidy.medappnem.videoActivities.BrokenBoneVideoActivity.java
gidy.medappnem.videoActivities.BurnVideoActivity.java
gidy.medappnem.videoActivities.ChokeActivityVideo.java
gidy.medappnem.videoActivities.DiabeticVideoActivity.java
gidy.medappnem.videoActivities.DistressVideoActivity.java
gidy.medappnem.videoActivities.EpilepsyVideoActivity.java
gidy.medappnem.videoActivities.HeadInjuryVideoActivity.java
gidy.medappnem.videoActivities.HeartAttackVideoActivity.java
gidy.medappnem.videoActivities.PoisoningVideoActivity.java
gidy.medappnem.videoActivities.StrokeVideoActivity.java
gidy.medappnem.videoActivities.UnconsciousBreatingVideoActivity.java
gidy.medappnem.videoActivities.UnconsciousNotBreathingVideoActivity.java
gidy.medappnem.videoActivities.VideoActivity.java