0

我正在尝试从 Android 获取 GPS 坐标并将其发送到 MySQL 数据库。

一切运行良好,但程序进入无限循环:因为我使用 While (true) 更新 GPS 坐标并每 5 秒将其发送到数据库。

如何避免循环并每 5 秒将坐标发送到数据库中。

这是我的代码:

package com.example.gpstracking;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;

public class AndroidGPSTrackingActivity extends Activity {
    Button btnShowLocation;

    GPSTracker gps;
    double tmplat=0;
    double tmplong=0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        while (true)
        {
            gps = new GPSTracker(AndroidGPSTrackingActivity.this);
            if(gps.canGetLocation()){
                double latitude = gps.getLatitude();
                double longitude = gps.getLongitude();
                String lat=String.valueOf(latitude);
                String lon=String.valueOf(longitude);
                ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
                postParameters.add(new BasicNameValuePair("latitude", lat));
                postParameters.add(new BasicNameValuePair("longitude", lon));
                try {   
                    CustomHttpClient.executeHttpPost("http://plangsm2012.site40.net/new/check.php", postParameters);
                } catch (Exception e) { 
                }
                tmplat=latitude;
                tmplong=longitude;  

            }

            else{
            gps.showSettingsAlert();
            }

            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
4

2 回答 2

0

在您的 locationlistener 中,填写您的 bean 并发布,如下所示:

@Override
public void onLocationChanged(Location arg0) {
    myLocation.setLatitude(Double.toString(arg0.getLatitude()));
    myLocation.setLongitude(Double.toString(arg0.getLongitude()));
    myLocation.setTimestamp(arg0.getTime());
    myLocation.setUserId(Secure.ANDROID_ID);
    if (new DateTime(arg0.getTime().minusMinutes(5) > new DateTime(oldTime)) 
        postToRemote(myLocation);
}

myLocation 是一个bean,包含纬度、经度、时间戳和设备 ID 的属性,由于遗留原因被错误地称为 userId。

至于每 5 秒发布一次,您需要记录它发布的时间,并将其与 post 调用之前的当前时间进行比较,如上面的 if 语句。

于 2013-07-22T15:46:36.440 回答
0

您可以使用计时器来解决此问题。

private void time() {

    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {

             gps = new GPSTracker(AndroidGPSTrackingActivity.this);
            if(gps.canGetLocation()){
                double latitude = gps.getLatitude();
                double longitude = gps.getLongitude();
                String lat=String.valueOf(latitude);
                String lon=String.valueOf(longitude);
                ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
                postParameters.add(new BasicNameValuePair("latitude", lat));
                postParameters.add(new BasicNameValuePair("longitude", lon));
                try {   
                    CustomHttpClient.executeHttpPost("http://plangsm2012.site40.net/new/check.php", postParameters);
                } catch (Exception e) { 
                }
                tmplat=latitude;
                tmplong=longitude;  

            }

            else{
            gps.showSettingsAlert();
            }

            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }, 5000); // 5 sec

}

干杯

于 2013-07-22T15:47:40.323 回答