Advertisement
Guest User

Untitled

a guest
Oct 25th, 2014
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. public class LocationService extends Service {
  2.  
  3. final static String TAG = "MyService";
  4. double latitude ;
  5. double Longitude ;
  6. LocationManager lm;
  7. LocationListener ll;
  8.  
  9. @Override
  10. public IBinder onBind(Intent intent) {
  11. return null;
  12. }
  13.  
  14. @Override
  15. public void onCreate() {
  16.  
  17. Log.d(TAG, "onCreate");
  18. super.onCreate();
  19. }
  20.  
  21. @Override
  22. public int onStartCommand(Intent intent, int flags, int startId) {
  23. new Thread() {
  24. public void run() {
  25. Looper.prepare();
  26. while(true){
  27. lm = (LocationManager) getSystemService(LOCATION_SERVICE);
  28. ll = new MyLocationListener();
  29.  
  30. lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,
  31. 0, 0, ll);
  32. //when i log here , it gives me wronganswer
  33. Log.d(TAG,Double.toString(latitude));
  34. try {
  35. Thread.sleep(3000);
  36. } catch (InterruptedException e) {
  37. e.printStackTrace();
  38. }
  39. }
  40. }
  41. }.start();
  42. return super.onStartCommand(intent, flags, startId);
  43. }
  44. @Override
  45. public void onDestroy() {
  46. Log.d(TAG, "OnDestroy");
  47. super.onDestroy();
  48. }
  49.  
  50. class MyLocationListener implements LocationListener{
  51. @Override
  52. public void onLocationChanged(Location location) {
  53. //when i log here , it gives me correct answer
  54. double lat = location.getLatitude();
  55. double lon = location.getLongitude();
  56. latitude = lat;
  57. Longitude = lon;
  58. }
  59. @Override
  60. public void onProviderDisabled(String arg0) {
  61. }
  62.  
  63. @Override
  64. public void onProviderEnabled(String arg0) {
  65. }
  66.  
  67. @Override
  68. public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
  69. }
  70. };
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement