在获取一定的时间间隔在Android设备上的位置数据 [英] Getting location data on an Android device in certain intervals

查看:252
本文介绍了在获取一定的时间间隔在Android设备上的位置数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个Android应用程序这需要在一定的时间间隔例如位置数据: - 5秒,1分钟,等等。这里是我的code: -

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();                                  
                              }

                  }
              }
            }

下面是code倒计时类: -
这个类用于间隔添加到应用

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);            
    }
}

问题是,使用上述code,我没有得到准确的时间间隔。我总是获得1秒额外(由于其中的式I中减去1000),但是这1秒不总是发生。因此,谁能告诉我什么我做错了?

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 ?

推荐答案

看<一个href=\"http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates%28long,%20float,%20android.location.Criteria,%20android.app.PendingIntent%29\"相对=nofollow> LocationManager.requestLocationUpdates 中,只是通过在参数的时间间隔。

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());

这篇关于在获取一定的时间间隔在Android设备上的位置数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆