Adam Baazouzi Adam Baazouzi - 7 months ago 268
SQL Question

How to send Android GPS coordinates to MySQL every 5 secondes

I'm trying to get GPS coordinates from Android and send it to MySQL database.

Everything is working well, but the program enters into unlimited loop :because I used While (true) to updates the GPS coordinates and send it to Database every 5 secondes.

How can I avoid the loop and send the coordinates into database every 5 secondes. help me please

here is my code:

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

Answer

You can use a timer for this problem.

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

}

Cheers