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 ?

4

3 回答 3

2

查看LocationManager.requestLocationUpdates,只需在参数中传递您的时间间隔..

LocationManager mLocationManager = (LocationManager).getSystemService(mActivity.LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 0, new GeoUpdateHandler());
于 2013-05-22T05:23:03.267 回答
1

我认为这里你需要使用默认功能,不需要使用Timer

NETWORK_PROVIDER

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

GPS_PROVIDER

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

这里 ,

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

文档

于 2013-05-22T05:24:26.233 回答
0

我不认为 requestLocationUpdates(...) 方法的 minTime 参数有任何作用。它似乎并没有阻止基于距离的更新以更短的时间间隔出现,而且它绝对不会像许多人认为的那样导致更新以指定的时间间隔出现。我已经在 Android 2.3.6 到 4.0.4 的设备上尝试过。

于 2013-10-28T05:34:49.393 回答