Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. public class GPS extends Service {
  2. public static final int notify = 1000*60*5; //interval between two services(Here Service run every 5 Minute)
  3. private Handler mHandler = new Handler();
  4. private Timer mTimer = null;
  5.  
  6. private LocationManager locationMangaer = null;
  7. private LocationListener locationListener = null;
  8. private static final String TAG = "Debug";
  9. private Boolean flag = false;
  10.  
  11. @Override
  12. public IBinder onBind(Intent intent) {
  13. throw new UnsupportedOperationException("Not yet implemented");
  14. }
  15.  
  16. @Override
  17. public void onCreate() {
  18. if (mTimer != null)
  19. mTimer.cancel();
  20. else {
  21. mTimer = new Timer();
  22. mTimer.scheduleAtFixedRate(new TimeDisplay(), 0, notify);
  23. }
  24.  
  25. locationMangaer = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
  26.  
  27. }
  28.  
  29.  
  30. private Boolean displayGpsStatus() {
  31. ContentResolver contentResolver = getBaseContext().getContentResolver();
  32. boolean gpsStatus = Settings.Secure.isLocationProviderEnabled(contentResolver,LocationManager.GPS_PROVIDER);
  33. if (gpsStatus) {
  34. return true;
  35.  
  36. } else {
  37. return false;
  38. }
  39. }
  40.  
  41. public class MyLocationListener implements LocationListener {
  42. dbISMLock dbismlock = new dbISMLock(getBaseContext());
  43. final SQLiteDatabase db =dbismlock.getWritableDatabase();
  44.  
  45. @Override
  46. public void onLocationChanged(Location loc) {
  47.  
  48.  
  49.  
  50. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  51. String currentDateandTime = sdf.format(new Date());
  52.  
  53. Toast.makeText(getBaseContext(),"fecha: "+ currentDateandTime +"Location changed : Lat: " + loc.getLatitude()+ " Lng: " + loc.getLongitude(),Toast.LENGTH_SHORT).show();
  54. String longitude = "Longitude: " +loc.getLongitude();
  55. Log.v(TAG, longitude);
  56. String latitude = "Latitude: " +loc.getLatitude();
  57. Log.v(TAG, latitude);
  58.  
  59.  
  60. TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
  61. String phoneID = telephonyManager.getDeviceId();
  62.  
  63. //INSERT
  64. db.execSQL("INSERT INTO GEOLOCATION( phoneId,Fecha,longitude, latitude) VALUES('"+phoneID+"','"+currentDateandTime+"','"+longitude+"','"+latitude+"')");
  65. Log.d("insertamos "," geolocation" );
  66.  
  67. }
  68.  
  69. @Override
  70. public void onProviderDisabled(String provider) {
  71.  
  72. }
  73.  
  74. @Override
  75. public void onProviderEnabled(String provider) {
  76.  
  77. }
  78.  
  79. @Override
  80. public void onStatusChanged(String provider, int status, Bundle extras) {
  81.  
  82. }
  83.  
  84.  
  85.  
  86.  
  87. }
  88.  
  89.  
  90. @Override
  91. public void onDestroy() {
  92. super.onDestroy();
  93. mTimer.cancel();
  94. Toast.makeText(this, "Service is Destroyed", Toast.LENGTH_SHORT).show();
  95. }
  96.  
  97.  
  98. class TimeDisplay extends TimerTask {
  99. @Override
  100. public void run() {
  101.  
  102. mHandler.post(new Runnable() {
  103. @Override
  104. public void run() {
  105.  
  106. Toast.makeText(GPS.this, coordenadas(), Toast.LENGTH_SHORT).show();
  107.  
  108. flag = displayGpsStatus();
  109. if (flag) {
  110.  
  111. Log.v(TAG, "onClick");
  112.  
  113.  
  114. locationListener = new MyLocationListener();
  115.  
  116.  
  117. if (ActivityCompat.checkSelfPermission(GPS.this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(GPS.this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  118.  
  119. return;
  120. }
  121. locationMangaer.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, locationListener);
  122.  
  123.  
  124. } else {
  125. Log.d("Gps Status!!", "Your GPS is: OFF");
  126. }
  127.  
  128. }
  129. });
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement