Advertisement
Guest User

GPSTracker.java

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