GPS Location Updates

Put your problem here if it does not fit any of the other categories.

GPS Location Updates

Postby MOnline » Sun Oct 04, 2009 10:21 pm

Hi All,

I am currently developing a simple trace application (just for the fun of it and learning android). Because I want my program to be usable when either driving a vehicle / bike or walking I wanted to get my location updates every 2 seconds (meaning 2000ms but you guys / girls will already know that ;)). So I created a service and as soon as it starts it starts requesting location updates. Below is my exact code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 2000, 0, serviceDataListener);
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


As far as I can see this should be correct. But when i checked my logged data it is updated every second. My question is if I need to set a minDistance for updating or not? Also do you think 2 seconds for tracing is 2 fast?

My program drains a lot of power (probally due to the quick update ;)). But even when I close my service I looks like it keeps draining power. I will post some snippets below maybe one of you can see whats wrong:

On Create
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void onCreate()
  2.  
  3.     {
  4.  
  5.         super.onCreate();
  6.  
  7.         bInitialized = false;
  8.  
  9.         bTraceRunning = false;
  10.  
  11.         traceInfo = new TraceInfo("");
  12.  
  13.  
  14.  
  15.         previousLocation = null;
  16.  
  17.         locationManager = null;
  18.  
  19.         serviceDataListener = null;
  20.  
  21.         guiUpdateListener = null;
  22.  
  23.     }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


On Destroy
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2.  
  3.     public void onDestroy()
  4.  
  5.     {
  6.  
  7.         super.onDestroy();
  8.  
  9.  
  10.  
  11.         if (serviceDataListener != null)
  12.  
  13.             locationManager.removeUpdates(serviceDataListener);
  14.  
  15.  
  16.  
  17.         locationManager = null;
  18.  
  19.         serviceDataListener = null;
  20.  
  21.         traceInfo = null;
  22.  
  23.         previousLocation = null;
  24.  
  25.         serviceDataListener = null;
  26.  
  27.         guiUpdateListener = null;
  28.  
  29.     }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


On Start
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. super.onStart(intent, startId);
  2.  
  3.         if (!bInitialized)
  4.  
  5.         {
  6.  
  7.             // Setup location Manager
  8.  
  9.             locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  10.  
  11.             serviceDataListener = new LocationListener() {.......};
  12.  
  13.  
  14.  
  15.             locationManager.requestLocationUpdates(
  16.  
  17.                     LocationManager.GPS_PROVIDER, 2000, 0, serviceDataListener);
  18.  
  19.  
  20.  
  21.             bInitialized = true;
  22.  
  23.         }
  24.  
  25.     }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


On Client Bind / UnBind
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2.  
  3.     public IBinder onBind(Intent intent)
  4.  
  5.     {
  6.  
  7.         return mBinder;
  8.  
  9.     }
  10.  
  11.  
  12.  
  13.     @Override
  14.  
  15.     public boolean onUnbind(Intent intent)
  16.  
  17.     {
  18.  
  19.         checkServiceStatus();
  20.  
  21.         return super.onUnbind(intent);
  22.  
  23.     }
  24.  
  25.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Check Service Status
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private boolean checkServiceStatus()
  2.  
  3.     {
  4.  
  5.         if (!bTraceRunning)
  6.  
  7.         {
  8.  
  9.             locationManager.removeUpdates(serviceDataListener);
  10.  
  11.             this.stopSelf();
  12.  
  13.             return false;
  14.  
  15.         }
  16.  
  17.         else{
  18.  
  19.             return true;
  20.  
  21.         }
  22.  
  23.     }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


Hope someone can help,

With kind regards,

MOnline

PS
about the battery drainage when running the service one (tracing for about 2 minutes) the battery goes from fully charged to 5% in about 10 hours (normally it lasts about 1,5 day (sometimes 2 ;))).
MOnline
Junior Developer
Junior Developer
 
Posts: 11
Joined: Mon Sep 21, 2009 9:51 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 23 guests