Advertisement
rizkipm

Untitled

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