如何在屏幕上定期更新Android GPS?

时间:2013-06-06 19:40:33

标签: android

我正在尝试测量单击开始按钮时的行程,并在单击停止按钮后停止测量。要做到这一点 (1)我已经设置了位置管理器,位置监听和位置对象,并且在我放置onClick Listener之前它们已经运行良好。 (2)设置onClickListener后,GPS不再定期更新。有谁知道如何在事件触发后定期更新GPS?以下是我的代码。

public class MeasureDistance extends Activity implements LocationListener {
    TextView showData;
    Button start;
    Button stop;
    double longitutde;
    double previousLongitude;
    Location previous;
    Location current;
    LocationListener myListener;
    Location location;
    Location previousLocation = null;
    LocationManager locationManager;
    static double meter = 0;
    double distance = 0;
    boolean startGPS = false;
    double diff;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {   //This is for the UI
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        showData = (TextView)findViewById(R.id.showText);
        start = (Button)findViewById(R.id.startButton);
        stop = (Button)findViewById(R.id.stopButton);

        start.setOnClickListener(onClickListener);
        stop.setOnClickListener(onClickListener);
        //This is for the Location
        locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER,0,0,this);
    }

    public void onLocationChanged(Location location)
    {
        if((previousLocation != location) && (previousLocation != null) &&(startGPS == true))
        {
            distance = location.distanceTo(previousLocation);
            meter = distance + meter;
        }
        previousLocation = location;
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public void onProviderEnabled(String provider) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public void onProviderDisabled(String provider) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public View.OnClickListener onClickListener = new View.OnClickListener()
    {
        public void onClick(final View v){
            switch(v.getId())
            {
                case R.id.startButton:
                {
                    startGPS = true;
                    showData.setText(Double.toString(meter * 3.28));
                    break;
                }
                case R.id.stopButton:
                {
                    showData.setText("Stop Button is Click");
                    break;
                }
            }
        }
        //To change body of implemented methods use File | Settings | File Templates.
    } ;
}

1 个答案:

答案 0 :(得分:1)

我假设您要实现的是:1)用户按下“开始”,该位置开始使用gps进行更新。 2)只要用户没有按下停止,距离就会继续更新3)在停止时,位置和距离不再更新。

For 1):您应该在Start Button的onClick()方法中注册位置监听器。目前,您正在onCreate()中注册位置监听器。因此,按下“开始”按钮将不执行任何操作(除了在showData textview中显示距离)。

For 2):让距离计算在onLocationChanged()方法中。您应该移动显示代码,即将showData textview中的距离设置为onLocationChanged()方法。目前,您的代码仅在“开始”按钮单击时使用计算的距离更新showData文本。

对于3):在停止按钮onClick()方法上,您应取消注册位置监听器,以便您不要求位置更新(当用户想要停止时)。

locationManager.removeUpdates(this);