Guest User

Untitled

a guest
Jul 3rd, 2015
498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.00 KB | None | 0 0
  1. package com.tiesna.tracker;
  2.  
  3. import java.io.IOException;
  4. import java.util.List;
  5. import java.util.Locale;
  6.  
  7. import com.tiesna.tracker.R;
  8.  
  9. import android.app.AlertDialog;
  10. import android.app.Service;
  11. import android.content.Context;
  12. import android.content.DialogInterface;
  13. import android.content.Intent;
  14. import android.location.Address;
  15. import android.location.Geocoder;
  16. import android.location.Location;
  17. import android.location.LocationListener;
  18. import android.location.LocationManager;
  19. import android.os.Bundle;
  20. import android.os.IBinder;
  21. import android.provider.Settings;
  22. import android.util.Log;
  23.  
  24. public class GPSTracker extends Service implements LocationListener
  25. {
  26.     private final Context mContext;
  27.  
  28.     //flag for GPS Status
  29.     boolean isGPSEnabled = false;
  30.  
  31.     //flag for network status
  32.     boolean isNetworkEnabled = false;
  33.  
  34.     boolean canGetLocation = false;
  35.  
  36.     Location location;
  37.     double latitude;
  38.     double longitude;
  39.  
  40.     //minimal jarak update 10 meter
  41.     private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; //10 meter
  42.  
  43.     //minimal update waktu dalam milliseconds
  44.     private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 menit
  45.  
  46.     //Declaring a Location Manager
  47.     protected LocationManager locationManager;
  48.  
  49.     public GPSTracker(Context context)
  50.     {
  51.         this.mContext = context;
  52.         getLocation();
  53.     }
  54.  
  55.     public Location getLocation()
  56.     {
  57.         try
  58.         {
  59.             locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
  60.  
  61.             //getting GPS status
  62.             isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
  63.  
  64.             //getting network status
  65.             isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  66.  
  67.             if (!isGPSEnabled && !isNetworkEnabled)
  68.             {
  69.                 // no network provider is enabled
  70.             }
  71.             else
  72.             {
  73.                 this.canGetLocation = true;
  74.  
  75.                 //First get location from Network Provider
  76.                 if (isNetworkEnabled)
  77.                 {
  78.                     locationManager.requestLocationUpdates(
  79.                             LocationManager.NETWORK_PROVIDER,
  80.                             MIN_TIME_BW_UPDATES,
  81.                             MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
  82.                     if (locationManager != null)
  83.                     {
  84.                         location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  85.                         updateGPSCoordinates();
  86.                     }
  87.                 }
  88.  
  89.                 //if GPS Enabled get lat/long using GPS Services
  90.                 if (isGPSEnabled)
  91.                 {
  92.                     if (location == null)
  93.                     {
  94.                         locationManager.requestLocationUpdates(
  95.                                 LocationManager.GPS_PROVIDER,
  96.                                 MIN_TIME_BW_UPDATES,
  97.                                 MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
  98.  
  99.                         Log.d("GPS Enabled", "GPS Enabled");
  100.  
  101.                         if (locationManager != null)
  102.                         {
  103.                             location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  104.                             updateGPSCoordinates();
  105.                         }
  106.                     }
  107.                 }
  108.             }
  109.         }
  110.         catch (Exception e)
  111.         {
  112.             //e.printStackTrace();
  113.             Log.e("Error : Location", "Impossible to connect to LocationManager", e);
  114.         }
  115.  
  116.         return location;
  117.     }
  118.  
  119.     public void updateGPSCoordinates()
  120.     {
  121.         if (location != null)
  122.         {
  123.             latitude = location.getLatitude();
  124.             longitude = location.getLongitude();
  125.         }
  126.     }
  127.  
  128.     /**
  129.      * Stop using GPS listener
  130.      * Calling this function will stop using GPS in your app
  131.      */
  132.  
  133.     public void stopUsingGPS()
  134.     {
  135.         if (locationManager != null)
  136.         {
  137.             locationManager.removeUpdates(GPSTracker.this);
  138.         }
  139.     }
  140.  
  141.     /**
  142.      * Function to get latitude
  143.      */
  144.     public double getLatitude()
  145.     {
  146.         if (location != null)
  147.         {
  148.             latitude = location.getLatitude();
  149.         }
  150.  
  151.         return latitude;
  152.     }
  153.  
  154.     /**
  155.      * Function to get longitude
  156.      */
  157.     public double getLongitude()
  158.     {
  159.         if (location != null)
  160.         {
  161.             longitude = location.getLongitude();
  162.         }
  163.  
  164.         return longitude;
  165.     }
  166.  
  167.     /**
  168.      * Function to check GPS/wifi enabled
  169.      */
  170.     public boolean canGetLocation()
  171.     {
  172.         return this.canGetLocation;
  173.     }
  174.  
  175.     /**
  176.      * Function to show settings alert dialog
  177.      */
  178.     public void showSettingsAlert()
  179.     {
  180.         AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
  181.  
  182.         //Setting Dialog Title
  183.        // alertDialog.setTitle(R.string.GPSAlertDialogTitle);
  184.  
  185.  
  186.         //Setting Dialog Message
  187.       //  alertDialog.setMessage(R.string.GPSAlertDialogMessage);
  188.  
  189.         //On Pressing Setting button
  190.         alertDialog.setPositiveButton(R.string.action_settings, new DialogInterface.OnClickListener()
  191.         {  
  192.             @Override
  193.             public void onClick(DialogInterface dialog, int which)
  194.             {
  195.                 Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  196.                 mContext.startActivity(intent);
  197.             }
  198.         });
  199.  
  200.         //On pressing cancel button
  201. //        //alertDialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener()
  202. //        {  
  203. //            @Override
  204. //            public void onClick(DialogInterface dialog, int which)
  205. //            {
  206. //                dialog.cancel();
  207. //            }
  208. //        });
  209.  
  210.         alertDialog.show();
  211.     }
  212.  
  213.     /**
  214.      * Get list of address by latitude and longitude
  215.      * @return null or List<Address>
  216.      */
  217.     public List<Address> getGeocoderAddress(Context context)
  218.     {
  219.         if (location != null)
  220.         {
  221.             Geocoder geocoder = new Geocoder(context, Locale.ENGLISH);
  222.             try
  223.             {
  224.                 List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
  225.                 return addresses;
  226.             }
  227.             catch (IOException e)
  228.             {
  229.                 //e.printStackTrace();
  230.                 Log.e("Error : Geocoder", "Impossible to connect to Geocoder", e);
  231.             }
  232.         }
  233.  
  234.         return null;
  235.     }
  236.  
  237.     /**
  238.      * Try to get AddressLine
  239.      * @return null or addressLine
  240.      */
  241.     public String getAddressLine(Context context)
  242.     {
  243.         List<Address> addresses = getGeocoderAddress(context);
  244.         if (addresses != null && addresses.size() > 0)
  245.         {
  246.             Address address = addresses.get(0);
  247.             String addressLine = address.getAddressLine(0);
  248.  
  249.             return addressLine;
  250.         }
  251.         else
  252.         {
  253.             return null;
  254.         }
  255.     }
  256.  
  257.     /**
  258.      * Try to get Locality
  259.      * @return null or locality
  260.      */
  261.     public String getLocality(Context context)
  262.     {
  263.         List<Address> addresses = getGeocoderAddress(context);
  264.         if (addresses != null && addresses.size() > 0)
  265.         {
  266.             Address address = addresses.get(0);
  267.             String locality = address.getLocality();
  268.  
  269.             return locality;
  270.         }
  271.         else
  272.         {
  273.             return null;
  274.         }
  275.     }
  276.  
  277.     /**
  278.      * Try to get Postal Code
  279.      * @return null or postalCode
  280.      */
  281.     public String getPostalCode(Context context)
  282.     {
  283.         List<Address> addresses = getGeocoderAddress(context);
  284.         if (addresses != null && addresses.size() > 0)
  285.         {
  286.             Address address = addresses.get(0);
  287.             String postalCode = address.getPostalCode();
  288.  
  289.             return postalCode;
  290.         }
  291.         else
  292.         {
  293.             return null;
  294.         }
  295.     }
  296.  
  297.     /**
  298.      * Try to get CountryName
  299.      * @return null or postalCode
  300.      */
  301.     public String getCountryName(Context context)
  302.     {
  303.         List<Address> addresses = getGeocoderAddress(context);
  304.         if (addresses != null && addresses.size() > 0)
  305.         {
  306.             Address address = addresses.get(0);
  307.             String countryName = address.getCountryName();
  308.  
  309.             return countryName;
  310.         }
  311.         else
  312.         {
  313.             return null;
  314.         }
  315.     }
  316.  
  317.     @Override
  318.     public void onLocationChanged(Location location)
  319.     {  
  320.     }
  321.  
  322.     @Override
  323.     public void onProviderDisabled(String provider)
  324.     {  
  325.     }
  326.  
  327.     @Override
  328.     public void onProviderEnabled(String provider)
  329.     {  
  330.     }
  331.  
  332.     @Override
  333.     public void onStatusChanged(String provider, int status, Bundle extras)
  334.     {  
  335.     }
  336.  
  337.     @Override
  338.     public IBinder onBind(Intent intent)
  339.     {
  340.         return null;
  341.     }
  342. }
Advertisement
Add Comment
Please, Sign In to add comment