1

I am trying to make an Android app which takes the location data in certain intervals e.g:- 5 sec, 1 min,etc. Here is my code :-

            public void onLocationChanged(Location loc) {
            // TODO Auto-generated method stub
            if(loc!=null)
            {   
              //Required Interval
              tInterval =(minInterval*60*1000) + (secInterval)*1000 - 1000;

              //The app also supports interval mode
              RadioButton sel = (RadioButton) findViewById(mode.getCheckedRadioButtonId()); 

              //Code for Manual Functionality
              if(sel.getText().equals(MANUAL_RADIO))
              {
                      Time t = new Time();
                      t.setToNow();
                      db.addLocationAtTime(loc, t);
                      Toast.makeText(getApplicationContext(), "Location Added to Database",Toast.LENGTH_SHORT).show();        
                      locate.removeUpdates(this);

                      b.setText(MANUAL_BUTTON);
                      d.setEnabled(true);
              }

              //Code for Interval functionality
              else if(sel.getText().equals(INTERVAL_RADIO))
              {
                              //count is object of Countdown class which is a Thread object
                              if(count == null)
                              {
                                      //t is a Time object
                                      t.setToNow();
                                      //SQLiteDatabase object for logging Location with Time
                                      db.addLocationAtTime(loc, t);
                                      Toast.makeText(getApplicationContext(), "Location Added to Database",Toast.LENGTH_SHORT).show();
                                      count =  new CountDown( tInterval);
                                      count.start();
                              }

                              else if(count.getState().toString().equals("TERMINATED"))
                              {
                                      t.setToNow();
                                      db.addLocationAtTime(loc, t);
                                      Toast.makeText(getApplicationContext(), "Location Added to Database",Toast.LENGTH_SHORT).show();
                                      count =  new CountDown(tInterval);
                                      count.start();                                  
                              }

                  }
              }
            }

Here is the code for the Countdown class:- This class is used to add the interval to the app

public class CountDown extends Thread 
{
    long time;
    public CountDown(long duration)
    {
            time = duration;
    }

    public void run()
    {
                    long t1 = System.currentTimeMillis();
                    long t2 = 0;
                    do
                    {
                      t2 = System.currentTimeMillis();      
                    }while(t2 - t1 < time);            
    }
}

The problem is that using the above code I am not getting accurate intervals. I am always getting 1 sec extra (due to which I subtracted 1000 in the formula) , but this 1 sec is not happening always. So can anyone please tell me what I am doing wrong ?

3 Answers3

2

Look at LocationManager.requestLocationUpdates, just pass your time interval in parameter..

LocationManager mLocationManager = (LocationManager).getSystemService(mActivity.LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 0, new GeoUpdateHandler());
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
  • I am using only GPS Provider as there may not be proper network coverage where it is going to be used and secondly I need to have the GPS fix during the usage and setting the minTime in requestLocationUpdates() removes the fix until the time has elapsed. – Sourabh Bollapragada May 22 '13 at 05:31
  • you can also use "LocationManager.GPS_PROVIDER" – Niranj Patel May 22 '13 at 05:34
  • I used that, I need the GPS fix to stay ON due to which I set the minTime to 0 and added an interval using the Countdown class, but I am getting a 1 sec difference due to which I subtracted 1000, but this difference is not happening for every time due to which I am getting locations in non uniform intervals. – Sourabh Bollapragada May 22 '13 at 05:54
  • time interval will be work when you location will changed... I hope you are stay same place thats why your method not calling... – Niranj Patel May 22 '13 at 06:04
  • when I set the time interval to say 5000 the GPS fix is going off for the 5000ms after getting the location. I am wasting time in getting the fix again which I dont want to do. So I set the interval to 0 so that the device has the fix and keeps logging locations in specified intervals determined by the Countdown object until I press the stop button in the app – Sourabh Bollapragada May 22 '13 at 06:21
1

i think here you need to use default features , no need to use Timer

NETWORK_PROVIDER

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 3000, 0, networkLocationListener);

GPS_PROVIDER

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 0, gpsLocationListener);

Here ,

minTime(2nd field)      =>   minimum time interval between location updates, in milliseconds
minDistance(3rd field)  =>   minimum distance between location updates, in meters

Documentation

Ronak Mehta
  • 5,971
  • 5
  • 42
  • 69
0

I don't think the minTime param of the requestLocationUpdates(...) methods does anything at all. It does not seem to prevent distance-based updates from coming at shorter intervals, and it definitely does not cause updates to come at the specified interval, as many people seem to think. I've tried it on devices from Android 2.3.6 to 4.0.4.

Kevin Krumwiede
  • 9,868
  • 4
  • 34
  • 82