Advertisement
h1man5hu

GPSTracker.java

Jul 10th, 2013
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.34 KB | None | 0 0
  1. package com.basic.mileagecalculatorwithsettings;
  2.  
  3. import android.app.AlertDialog;
  4. import android.app.Service;
  5. import android.content.Context;
  6. import android.content.DialogInterface;
  7. import android.content.Intent;
  8. import android.location.Location;
  9. import android.location.LocationListener;
  10. import android.location.LocationManager;
  11. import android.os.Bundle;
  12. import android.os.IBinder;
  13. import android.provider.Settings;
  14. import android.util.Log;
  15.  
  16. public class GPSTracker extends Service implements LocationListener
  17. {
  18.  
  19.     private final Context mContext;
  20.  
  21.     // flag for GPS status
  22.     boolean isGPSEnabled = false;
  23.  
  24.     // flag for network status
  25.     boolean isNetworkEnabled = false;
  26.  
  27.     // flag for GPS status
  28.     boolean canGetLocation = false;
  29.  
  30.     Location location; // location
  31.     double latitude; // latitude
  32.     double longitude; // longitude
  33.  
  34.     // The minimum distance to change Updates in meters
  35.     private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 5; // 5 meters
  36.  
  37.     // The minimum time between updates in milliseconds
  38.     private static final long MIN_TIME_BW_UPDATES = 1000 * 5; // 5 seconds
  39.  
  40.     // Declaring a Location Manager
  41.     protected LocationManager locationManager;
  42.  
  43.     public GPSTracker(Context context)
  44.     {
  45.         this.mContext = context;
  46.         getLocation();
  47.     }
  48.  
  49.     public Location getLocation()
  50.     {
  51.         try
  52.         {
  53.             locationManager = (LocationManager) mContext
  54.                     .getSystemService(LOCATION_SERVICE);
  55.  
  56.             // getting GPS status
  57.             isGPSEnabled = locationManager
  58.                     .isProviderEnabled(LocationManager.GPS_PROVIDER);
  59.  
  60.             // getting network status
  61.             isNetworkEnabled = locationManager
  62.                     .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  63.  
  64.             if (!isGPSEnabled && !isNetworkEnabled)
  65.             {
  66.                 // no network provider is enabled
  67.             } else
  68.             {
  69.                 this.canGetLocation = true;
  70.                 if (isNetworkEnabled)
  71.                 {
  72.                     locationManager.requestLocationUpdates(
  73.                             LocationManager.NETWORK_PROVIDER,
  74.                             MIN_TIME_BW_UPDATES,
  75.                             MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
  76.                     Log.d("Network", "Network");
  77.                     if (locationManager != null)
  78.                     {
  79.                         location = locationManager
  80.                                 .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  81.                         if (location != null)
  82.                         {
  83.                             latitude = location.getLatitude();
  84.                             longitude = location.getLongitude();
  85.                         }
  86.                     }
  87.                 }
  88.                 // if GPS Enabled get lat/long using GPS Services
  89.                 if (isGPSEnabled)
  90.                 {
  91.                     if (location == null)
  92.                     {
  93.                         locationManager.requestLocationUpdates(
  94.                                 LocationManager.GPS_PROVIDER,
  95.                                 MIN_TIME_BW_UPDATES,
  96.                                 MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
  97.                         Log.d("GPS Enabled", "GPS Enabled");
  98.                         if (locationManager != null)
  99.                         {
  100.                             location = locationManager
  101.                                     .getLastKnownLocation(LocationManager.GPS_PROVIDER);
  102.                             if (location != null)
  103.                             {
  104.                                 latitude = location.getLatitude();
  105.                                 longitude = location.getLongitude();
  106.                             }
  107.                         }
  108.                     }
  109.                 }
  110.             }
  111.  
  112.         }
  113.         catch (Exception e)
  114.         {
  115.             e.printStackTrace();
  116.         }
  117.  
  118.         return location;
  119.     }
  120.    
  121.     /**
  122.      * Stop using GPS listener
  123.      * Calling this function will stop using GPS in your app
  124.      * */
  125.     public void stopUsingGPS()
  126.     {
  127.         if(locationManager != null)
  128.         {
  129.             locationManager.removeUpdates(GPSTracker.this);
  130.         }      
  131.     }
  132.    
  133.     /**
  134.      * Function to get latitude
  135.      * */
  136.     public double getLatitude()
  137.     {
  138.         if(location != null)
  139.         {
  140.             latitude = location.getLatitude();
  141.         }
  142.        
  143.         // return latitude
  144.         return latitude;
  145.     }
  146.    
  147.     /**
  148.      * Function to get longitude
  149.      * */
  150.     public double getLongitude()
  151.     {
  152.         if(location != null)
  153.         {
  154.             longitude = location.getLongitude();
  155.         }
  156.        
  157.         // return longitude
  158.         return longitude;
  159.     }
  160.    
  161.     /**
  162.      * Function to check GPS/wifi enabled
  163.      * @return boolean
  164.      * */
  165.     public boolean canGetLocation()
  166.     {
  167.         return this.canGetLocation;
  168.     }
  169.    
  170.     /**
  171.      * Function to show settings alert dialog
  172.      * On pressing Settings button will lauch Settings Options
  173.      * */
  174.     public void showSettingsAlert()
  175.     {
  176.         AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
  177.      
  178.         // Setting Dialog Title
  179.         alertDialog.setTitle("GPS is not enabled");
  180.  
  181.         // Setting Dialog Message
  182.         alertDialog.setMessage("Enable location services to determine your location.");
  183.  
  184.         // On pressing Settings button
  185.         alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
  186.             public void onClick(DialogInterface dialog,int which)
  187.             {
  188.                 Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  189.                 mContext.startActivity(intent);
  190.             }
  191.         });
  192.  
  193.         // on pressing cancel button
  194.         alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  195.             public void onClick(DialogInterface dialog, int which)
  196.             {
  197.             dialog.cancel();
  198.             }
  199.         });
  200.  
  201.         // Showing Alert Message
  202.         alertDialog.show();
  203.     }
  204.  
  205.     @Override
  206.     public void onLocationChanged(Location location)
  207.     {
  208.    
  209.     }
  210.  
  211.     @Override
  212.     public void onProviderDisabled(String provider)
  213.     {
  214.     }
  215.  
  216.     @Override
  217.     public void onProviderEnabled(String provider)
  218.     {
  219.     }
  220.  
  221.     @Override
  222.     public void onStatusChanged(String provider, int status, Bundle extras)
  223.     {
  224.     }
  225.  
  226.     @Override
  227.     public IBinder onBind(Intent arg0)
  228.     {
  229.         return null;
  230.     }
  231.  
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement