Advertisement
Guest User

dd

a guest
Mar 25th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.13 KB | None | 0 0
  1. package id.co.imastudio.ojekmalanguser.helpers;
  2.  
  3. import android.Manifest;
  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.content.pm.PackageManager;
  10. import android.location.Location;
  11. import android.location.LocationListener;
  12. import android.location.LocationManager;
  13. import android.os.Build;
  14. import android.os.Bundle;
  15. import android.os.IBinder;
  16. import android.provider.Settings;
  17. import android.support.v4.app.ActivityCompat;
  18. import android.util.Log;
  19.  
  20.  
  21. public class GPSTracker extends Service implements LocationListener {
  22. private Context context = null;
  23. boolean isGPSEnabled = false, isNetworkEnabled = false,
  24. canGetLocation = false;
  25. Location location = null;
  26. double latitude, longitude;
  27.  
  28. private static final long MIN_DISTANCE = (long) 1; // 10 meter
  29. private static final long MIN_TIME = 1000 * 1 * 1; // 1minute
  30.  
  31. protected LocationManager locationManager;
  32. public static final String NEW_POSITION = "newPosition";
  33. //private SessionManager sesi;
  34.  
  35.  
  36. public GPSTracker(Context c) {
  37. this.context = c;
  38. //sesi = new SessionManager(c);
  39.  
  40. getLocation();
  41. }
  42.  
  43. private Location getLocation() {
  44. try {
  45. locationManager = (LocationManager) context
  46. .getSystemService(LOCATION_SERVICE);
  47.  
  48. // getting gps status
  49. isGPSEnabled = locationManager
  50. .isProviderEnabled(LocationManager.GPS_PROVIDER);
  51. // getting network status
  52. isNetworkEnabled = locationManager
  53. .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  54. //if (!isGPSEnabled && !isNetworkEnabled) {
  55. if (!isGPSEnabled && !isNetworkEnabled) {
  56. showSettingGps();
  57. } else {
  58. canGetLocation = true;
  59. // get lat/lng by network
  60. if (isNetworkEnabled) {
  61.  
  62. if (checkPermission(context)) {
  63. locationManager.requestLocationUpdates(
  64. LocationManager.NETWORK_PROVIDER, MIN_TIME,
  65. MIN_DISTANCE, this);
  66. Log.d("network", "network enabled");
  67. if (locationManager != null) {
  68. location = locationManager
  69. .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  70. if (location != null) {
  71. latitude = location.getLatitude();
  72. longitude = location.getLongitude();
  73. }
  74. }
  75. } else {
  76. RbHelper.pesan(context, "Permission for GPS not valid");
  77. }
  78. }
  79.  
  80. // get lat/lng by gps
  81. if (isGPSEnabled) {
  82. if (location == null) {
  83. if (checkPermission(context)) {
  84. locationManager.requestLocationUpdates(
  85. LocationManager.GPS_PROVIDER, MIN_TIME,
  86. MIN_DISTANCE, this);
  87. Log.d("GPS", "GPS enabled");
  88. if (locationManager != null) {
  89. location = locationManager
  90. .getLastKnownLocation(LocationManager.GPS_PROVIDER);
  91. if (location != null) {
  92. latitude = location.getLatitude();
  93. longitude = location.getLongitude();
  94. }
  95.  
  96. }
  97. } else {
  98. RbHelper.pesan(context, "Permission for GPS not valid");
  99. }
  100. }
  101.  
  102. }
  103. }
  104. } catch (Exception e) {
  105. e.printStackTrace();
  106. }
  107.  
  108. return location;
  109.  
  110. }
  111.  
  112. /**
  113. * Stop using GPS listener Calling this function will stop using GPS in your
  114. * app
  115. */
  116. public void stopUsingGPS() {
  117. if (locationManager != null) {
  118. if (checkPermission(context)) {
  119. locationManager.removeUpdates(this);
  120. }
  121. }
  122. }
  123.  
  124. public double getLatitude() {
  125. if (location != null) {
  126. latitude = location.getLatitude();
  127. }
  128.  
  129. return latitude;
  130. }
  131.  
  132. public Location getLocations() {
  133. if (location != null) {
  134. return location;
  135. }
  136.  
  137. return null;
  138. }
  139.  
  140. public double getLongitude() {
  141. if (location != null) {
  142. longitude = location.getLongitude();
  143. }
  144.  
  145. return longitude;
  146. }
  147.  
  148. /**
  149. * Function to check GPS/wifi enabled
  150. *
  151. * @return boolean
  152. */
  153. public boolean canGetLocation() {
  154. return canGetLocation;
  155. }
  156.  
  157. /**
  158. * Function to show settings alert dialog On pressing Settings button will
  159. * lauch Settings Options
  160. */
  161. public void showSettingGps() {
  162. AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
  163.  
  164. alertBuilder.setTitle("GPS Setting");
  165. alertBuilder.setMessage("GPS is not enabled. Do you want to go to settings menu?");
  166.  
  167. alertBuilder.setPositiveButton("Setting", new DialogInterface.OnClickListener() {
  168.  
  169. @Override
  170. public void onClick(DialogInterface dialog, int which) {
  171. Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  172. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  173. startActivity(intent);
  174.  
  175. }
  176. });
  177. alertBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  178.  
  179. public void onClick(DialogInterface dialog, int which) {
  180. dialog.cancel();
  181.  
  182. }
  183. });
  184. }
  185.  
  186. @Override
  187. public void onLocationChanged(Location location) {
  188. if (location != null) {
  189. //NurHelper.pesan(context, "perubahan Alamat");
  190.  
  191.  
  192. if (this.location != location) {
  193. sendPosisi(location.getLatitude(), location.getLongitude());
  194. this.location = location;
  195. }
  196.  
  197.  
  198. }
  199.  
  200.  
  201. }
  202.  
  203. @Override
  204. public void onProviderDisabled(String provider) {
  205. // TODO Auto-generated method stub
  206.  
  207. }
  208.  
  209. @Override
  210. public void onProviderEnabled(String provider) {
  211. // TODO Auto-generated method stub
  212.  
  213. }
  214.  
  215. @Override
  216. public void onStatusChanged(String provider, int status, Bundle extras) {
  217. // TODO Auto-generated method stub
  218.  
  219. }
  220.  
  221. @Override
  222. public IBinder onBind(Intent intent) {
  223. // TODO Auto-generated method stub
  224. return null;
  225. }
  226.  
  227.  
  228. public void sendPosisi(double lat, double lng) {
  229. Intent i = new Intent(NEW_POSITION);
  230. i.putExtra("lat", lat);
  231. i.putExtra("lng", lng);
  232. context.sendBroadcast(i);
  233.  
  234. }
  235.  
  236.  
  237. public static boolean checkPermission(final Context context) {
  238. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  239.  
  240.  
  241. return ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION)
  242. == PackageManager.PERMISSION_GRANTED
  243. && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION)
  244. == PackageManager.PERMISSION_GRANTED;
  245. } else {
  246. return true;
  247. }
  248. }
  249.  
  250.  
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement