【问题标题】:How to send Android GPS coordinates to MySQL every 5 seconds如何每 5 秒将 Android GPS 坐标发送到 MySQL
【发布时间】:2018-08-05 10:25:22
【问题描述】:

我正在尝试从 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();
            }
        }
    }
}

【问题讨论】:

    标签: android mysql gps


    【解决方案1】:

    你可以使用计时器来解决这个问题。

    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
    
    }
    

    干杯

    【讨论】:

    • 感谢您的回答。我认为 Handler().postDelayed 用于延迟代码的执行,但是如何每 5 秒循环执行一次
    • NameValuePair 在较新版本的 android 中已弃用。
    【解决方案2】:

    在你的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秒发一次,你需要记录下它的发帖时间,并与上面if语句中的post调用前的当前时间进行比较。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-13
      • 2011-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多