You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LocationService.java 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. package com.example.meinwald.ui.area;
  2. import android.Manifest;
  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.content.pm.PackageManager;
  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. import com.example.meinwald.BuildConfig;
  17. import com.google.android.gms.maps.model.LatLng;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import androidx.core.app.ActivityCompat;
  21. import androidx.core.content.ContextCompat;
  22. //import static com.name.name.MapsActivity.NOTIFICATION_ID;
  23. public class LocationService extends Service implements LocationListener {
  24. private static final String TAG = "LocationService";
  25. private final Context mContext;
  26. // flag for GPS status
  27. boolean isGPSEnabled = false;
  28. // flag for network status
  29. boolean isNetworkEnabled = false;
  30. // flag for GPS status
  31. boolean canGetLocation = false;
  32. private boolean mLocationPermissionGranted;
  33. Location location; // location
  34. double latitude; // latitude
  35. double longitude; // longitude
  36. private List<LatLng> positions;
  37. // The minimum distance to change Updates in meters
  38. private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 1; // 5 meters
  39. // The minimum time between updates in milliseconds
  40. private static final long MIN_TIME_BW_UPDATES = 1000 * 1 * 1; // 5 seconds
  41. // Declaring a Location Manager
  42. protected LocationManager mLocationManager;
  43. public LocationService(Context context) {
  44. if (BuildConfig.DEBUG) {
  45. Log.d(TAG, "LocationService(Context context)");
  46. }
  47. this.mContext = context;
  48. this.positions = new ArrayList<>();
  49. this.mLocationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
  50. getLocationPermission();
  51. try {
  52. // getting GPS status
  53. isGPSEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
  54. if (!isGPSEnabled && !isNetworkEnabled) {
  55. // no network provider is enabled
  56. Log.d(TAG,"No permission to get location from any provider!");
  57. if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  58. }
  59. }
  60. else
  61. {
  62. Log.d(TAG,"Permission to get location from any provider!");
  63. this.canGetLocation = true;
  64. // First get location from Network Provider
  65. if (isNetworkEnabled) {
  66. if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  67. Log.d(TAG,"No permission to get location from network provider!");
  68. }
  69. if (BuildConfig.DEBUG)
  70. {
  71. Log.d(TAG, "getLocation() requestLocationUpdates() from GPS");
  72. }
  73. mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
  74. if (mLocationManager != null) {
  75. location = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  76. if (location != null) {
  77. positions.add(new LatLng(location.getLatitude(), location.getLongitude()));
  78. if (BuildConfig.DEBUG)
  79. {
  80. Log.d(TAG, "getLocation() new location added");
  81. }
  82. }
  83. }
  84. }
  85. // if GPS Enabled get lat/long using GPS Services
  86. if (isGPSEnabled && mLocationPermissionGranted) {
  87. if (location == null) {
  88. if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  89. Log.d(TAG,"No permission to get location from gps provider!");
  90. }
  91. if (BuildConfig.DEBUG)
  92. {
  93. Log.d(TAG, "getLocation() requestLocationUpdates() from GPS");
  94. }
  95. mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
  96. if (mLocationManager != null) {
  97. location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  98. if (location != null) {
  99. positions.add(new LatLng(location.getLatitude(), location.getLongitude()));
  100. if (BuildConfig.DEBUG)
  101. {
  102. Log.d(TAG, "getLocation() new location added");
  103. }
  104. }
  105. }
  106. }
  107. }
  108. }
  109. } catch (Exception e) {
  110. e.printStackTrace();
  111. }
  112. }
  113. public void getLocation() {
  114. if (BuildConfig.DEBUG) {
  115. Log.d(TAG, "getLocation()");
  116. }
  117. }
  118. /**
  119. * Stop using GPS listener
  120. * Calling this function will stop using GPS in your app
  121. * */
  122. public void stopUsingGPS(){
  123. if(mLocationManager != null){
  124. mLocationManager.removeUpdates(LocationService.this);
  125. }
  126. }
  127. /**
  128. * Function to get latitude
  129. * */
  130. public double getLatitude(){
  131. if(location != null){
  132. latitude = location.getLatitude();
  133. }
  134. // return latitude
  135. return latitude;
  136. }
  137. public List<LatLng> getPositions()
  138. {
  139. if (BuildConfig.DEBUG)
  140. {
  141. Log.d(TAG, "getPositions() count: " + this.positions.size());
  142. }
  143. return this.positions;
  144. }
  145. /**
  146. * Function to get longitude
  147. * */
  148. public double getLongitude(){
  149. if(location != null){
  150. longitude = location.getLongitude();
  151. }
  152. // return longitude
  153. return longitude;
  154. }
  155. /**
  156. * Function to check GPS/wifi enabled
  157. * @return boolean
  158. * */
  159. public boolean canGetLocation() {
  160. return this.canGetLocation;
  161. }
  162. /**
  163. * Function to show settings alert dialog
  164. * On pressing Settings button will lauch Settings Options
  165. * */
  166. public void showSettingsAlert(){
  167. AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
  168. // Setting Dialog Title
  169. alertDialog.setTitle("GPS is settings");
  170. // Setting Dialog Message
  171. alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
  172. // On pressing Settings button
  173. alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
  174. public void onClick(DialogInterface dialog,int which) {
  175. Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  176. mContext.startActivity(intent);
  177. }
  178. });
  179. // on pressing cancel button
  180. alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  181. public void onClick(DialogInterface dialog, int which) {
  182. dialog.cancel();
  183. }
  184. });
  185. // Showing Alert Message
  186. alertDialog.show();
  187. }
  188. /**
  189. * Request location permission, so that we can get the location of the device.
  190. * The result of the permission request is handled by a callback,
  191. * onRequestPermissionsResult.
  192. *
  193. */
  194. private void getLocationPermission() {
  195. if (ContextCompat.checkSelfPermission(mContext,
  196. android.Manifest.permission.ACCESS_FINE_LOCATION)
  197. == PackageManager.PERMISSION_GRANTED) {
  198. mLocationPermissionGranted = true;
  199. }
  200. else
  201. {
  202. mLocationPermissionGranted = false;
  203. }
  204. }
  205. @Override
  206. public void onLocationChanged(Location location) {
  207. if (BuildConfig.DEBUG)
  208. {
  209. Log.d(TAG, "onLocationChanged(), new location: " + location.toString());
  210. }
  211. positions.add(new LatLng(location.getLatitude(), location.getLongitude()));
  212. }
  213. @Override
  214. public void onProviderDisabled(String provider) {
  215. }
  216. @Override
  217. public void onProviderEnabled(String provider) {
  218. }
  219. @Override
  220. public void onStatusChanged(String provider, int status, Bundle extras) {
  221. }
  222. @Override
  223. public IBinder onBind(Intent arg0) {
  224. return null;
  225. }
  226. }