document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. GPSTracker.java
  2. package com.example.gpstracking;
  3.  
  4. import android.app.AlertDialog;
  5. import android.app.Service;
  6. import android.content.Context;
  7. import android.content.DialogInterface;
  8. import android.content.Intent;
  9. import android.location.Location;
  10. import android.location.LocationListener;
  11. import android.location.LocationManager;
  12. import android.os.Bundle;
  13. import android.os.IBinder;
  14. import android.provider.Settings;
  15. import android.util.Log;
  16.  
  17. public class GPSTracker extends Service implements LocationListener {
  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.    
  35.     private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
  36.  
  37.    
  38.     private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
  39.  
  40.     // Declaring a Location Manager
  41.     protected LocationManager locationManager;
  42.  
  43.     public GPSTracker(Context context) {
  44.         this.mContext = context;
  45.         getLocation();
  46.     }
  47.  
  48.     public Location getLocation() {
  49.         try {
  50.             locationManager = (LocationManager) mContext
  51.                     .getSystemService(LOCATION_SERVICE);
  52.  
  53.             // getting GPS status
  54.             isGPSEnabled = locationManager
  55.                     .isProviderEnabled(LocationManager.GPS_PROVIDER);
  56.  
  57.             // getting network status
  58.             isNetworkEnabled = locationManager
  59.                     .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  60.  
  61.             if (!isGPSEnabled && !isNetworkEnabled) {
  62.                 // no network provider is enabled
  63.             } else {
  64.                 this.canGetLocation = true;
  65.                 // First get location from Network Provider
  66.                 if (isNetworkEnabled) {
  67.                     locationManager.requestLocationUpdates(
  68.                             LocationManager.NETWORK_PROVIDER,
  69.                             MIN_TIME_BW_UPDATES,
  70.                             MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
  71.                     Log.d("Network", "Network");
  72.                     if (locationManager != null) {
  73.                         location = locationManager
  74.                                 .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  75.                         if (location != null) {
  76.                             latitude = location.getLatitude();
  77.                             longitude = location.getLongitude();
  78.                         }
  79.                     }
  80.                 }
  81.                 // if GPS Enabled get lat/long using GPS Services
  82.                 if (isGPSEnabled) {
  83.                     if (location == null) {
  84.                         locationManager.requestLocationUpdates(
  85.                                 LocationManager.GPS_PROVIDER,
  86.                                 MIN_TIME_BW_UPDATES,
  87.                                 MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
  88.                         Log.d("GPS Enabled", "GPS Enabled");
  89.                         if (locationManager != null) {
  90.                             location = locationManager
  91.                                     .getLastKnownLocation(LocationManager.GPS_PROVIDER);
  92.                             if (location != null) {
  93.                                 latitude = location.getLatitude();
  94.                                 longitude = location.getLongitude();
  95.                             }
  96.                         }
  97.                     }
  98.                 }
  99.             }
  100.  
  101.         } catch (Exception e) {
  102.             e.printStackTrace();
  103.         }
  104.  
  105.         return location;
  106.     }
  107.      
  108.     /**
  109.      * Stop using GPS listener
  110.      * Calling this function will stop using GPS in your app
  111.      * */
  112.     public void stopUsingGPS(){
  113.         if(locationManager != null){
  114.             locationManager.removeUpdates(GPSTracker.this);
  115.         }      
  116.     }
  117.      
  118.     /**
  119.      * Function to get latitude
  120.      * */
  121.     public double getLatitude(){
  122.         if(location != null){
  123.             latitude = location.getLatitude();
  124.         }
  125.          
  126.         // return latitude
  127.         return latitude;
  128.     }
  129.      
  130.     /**
  131.      * Function to get longitude
  132.      * */
  133.     public double getLongitude(){
  134.         if(location != null){
  135.             longitude = location.getLongitude();
  136.         }
  137.          
  138.         // return longitude
  139.         return longitude;
  140.     }
  141.      
  142.     /**
  143.      * Function to check GPS/wifi enabled
  144.      * @return boolean
  145.      * */
  146.     public boolean canGetLocation() {
  147.         return this.canGetLocation;
  148.     }
  149.      
  150.     /**
  151.      * Function to show settings alert dialog
  152.      * On pressing Settings button will lauch Settings Options
  153.      * */
  154.     public void showSettingsAlert(){
  155.         AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
  156.      
  157.         // Setting Dialog Title
  158.         alertDialog.setTitle("GPS is settings");
  159.  
  160.         // Setting Dialog Message
  161.         alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
  162.  
  163.         // On pressing Settings button
  164.         alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
  165.             public void onClick(DialogInterface dialog,int which) {
  166.                 Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  167.                 mContext.startActivity(intent);
  168.             }
  169.         });
  170.  
  171.         // on pressing cancel button
  172.         alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  173.             public void onClick(DialogInterface dialog, int which) {
  174.             dialog.cancel();
  175.             }
  176.         });
  177.  
  178.         // Showing Alert Message
  179.         alertDialog.show();
  180.     }
  181.  
  182.     @Override
  183.     public void onLocationChanged(Location location) {
  184.     }
  185.  
  186.     @Override
  187.     public void onProviderDisabled(String provider) {
  188.     }
  189.  
  190.     @Override
  191.     public void onProviderEnabled(String provider) {
  192.     }
  193.  
  194.     @Override
  195.     public void onStatusChanged(String provider, int status, Bundle extras) {
  196.     }
  197.  
  198.     @Override
  199.     public IBinder onBind(Intent arg0) {
  200.         return null;
  201.     }
  202.  
  203. }
');