Advertisement
riyanwenas

GPSTracker

Sep 21st, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.54 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. // First get location from Network Provider
  63. if (isNetworkEnabled) {
  64. locationManager.requestLocationUpdates(
  65. LocationManager.NETWORK_PROVIDER,
  66. MIN_TIME_BW_UPDATES,
  67. MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
  68. Log.d("Network", "Network");
  69. if (locationManager != null) {
  70. location = locationManager
  71. .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  72. if (location != null) {
  73. latitude = location.getLatitude();
  74. longitude = location.getLongitude();
  75. }
  76. }
  77. }
  78. // if GPS Enabled get lat/long using GPS Services
  79. if (isGPSEnabled) {
  80. if (location == null) {
  81. locationManager.requestLocationUpdates(
  82. LocationManager.GPS_PROVIDER,
  83. MIN_TIME_BW_UPDATES,
  84. MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
  85. Log.d("GPS Enabled", "GPS Enabled");
  86. if (locationManager != null) {
  87. location = locationManager
  88. .getLastKnownLocation(LocationManager.GPS_PROVIDER);
  89. if (location != null) {
  90. latitude = location.getLatitude();
  91. longitude = location.getLongitude();
  92. }
  93. }
  94. }
  95. }
  96. }
  97.  
  98. } catch (Exception e) {
  99. e.printStackTrace();
  100. }
  101.  
  102. return location;
  103. }
  104.  
  105. /**
  106. * Stop using GPS listener
  107. * Calling this function will stop using GPS in your app
  108. */
  109. public void stopUsingGPS() {
  110. if (locationManager != null) {
  111. locationManager.removeUpdates(GPSTracker.this);
  112. }
  113. }
  114.  
  115. /**
  116. * Function to get latitude
  117. */
  118. public double getLatitude() {
  119. if (location != null) {
  120. latitude = location.getLatitude();
  121. }
  122.  
  123. // return latitude
  124. return latitude;
  125. }
  126.  
  127. /**
  128. * Function to get longitude
  129. */
  130. public double getLongitude() {
  131. if (location != null) {
  132. longitude = location.getLongitude();
  133. }
  134.  
  135. // return longitude
  136. return longitude;
  137. }
  138.  
  139. /**
  140. * Function to check GPS/wifi enabled
  141. *
  142. * @return boolean
  143. */
  144. public boolean canGetLocation() {
  145. return this.canGetLocation;
  146. }
  147.  
  148. public boolean GPSActive() {
  149. if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
  150. return true;
  151. } else {
  152. return false;
  153. }
  154. }
  155.  
  156. /**
  157. * Function to show settings alert dialog
  158. * On pressing Settings button will lauch Settings Options
  159. */
  160. public void showSettingsAlert() {
  161. AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
  162. alertDialog.setCancelable(false);
  163. // Setting Dialog Title
  164. alertDialog.setTitle("GPS is settings");
  165. // Setting Dialog Message
  166. alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
  167.  
  168. // On pressing Settings button
  169. alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
  170. public void onClick(DialogInterface dialog, int which) {
  171. Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  172. mContext.startActivity(intent);
  173. }
  174. });
  175.  
  176. // on pressing cancel button
  177. alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  178. public void onClick(DialogInterface dialog, int which) {
  179. dialog.cancel();
  180. System.exit(0);
  181. }
  182. });
  183.  
  184. // Showing Alert Message
  185. alertDialog.show();
  186. }
  187.  
  188. @Override
  189. public void onLocationChanged(Location location) {
  190. }
  191.  
  192. @Override
  193. public void onProviderDisabled(String provider) {
  194. }
  195.  
  196. @Override
  197. public void onProviderEnabled(String provider) {
  198. }
  199.  
  200. @Override
  201. public void onStatusChanged(String provider, int status, Bundle extras) {
  202. }
  203.  
  204. @Override
  205. public IBinder onBind(Intent arg0) {
  206. return null;
  207. }
  208.  
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement