Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I invoke a method every 5 seconds in android?

I'm working in an application which must send a GPS location every 5 seconds to the server when I choose (auto send button on). I'm new with android so I don't know how I can make an on/off button and how can I invoke the method that sends the data every 5 seconds when the button is on.

The method which it must invoke every 5 seconds:

public void postData() throws ClientProtocolException, IOException, Exception {


    String longitude="UK";
    String latitude="UK";
    String altitiude="UK";
    String time="";
    String speed="";

    getCurrentLocation(); // gets the current location and update mobileLocation variables

    if (mobileLocation != null) {
        locManager.removeUpdates(locListener); // This needs to stop getting the location data and save the battery power.

         longitude = ""+mobileLocation.getLongitude();
         latitude = "" + mobileLocation.getLatitude();
         altitiude = "" + mobileLocation.getAltitude();
        String accuracy = "Accuracy" + mobileLocation.getAccuracy();
        time = "" + mobileLocation.getTime();
         speed =""+ (int)(4*mobileLocation.getSpeed());

        editTextShowLocation.setText(longitude + "\n" + latitude + "\n"
                + altitiude + "\n" + accuracy + "\n" + time+ "\n" + speed);
    } else {
        editTextShowLocation.setText("Sorry, location is not determined");

    }
        String url = "http://www.itrack.somee.com/post.aspx?id="+"f1"+"&long="+longitude+"&lat="+latitude+"&alt="+altitiude+"&speed="+speed;



        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);

        try {

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

        } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        } catch (IOException e) {
        // TODO Auto-generated catch block
        }



}
like image 921
Fadi Khalil Avatar asked Dec 17 '25 07:12

Fadi Khalil


2 Answers

I have faced exactly the same problem, sending location periodically. I've used a handler and its postDelayed method.

The periodic call part of my code looks like this:

private final int FIVE_SECONDS = 5000;
public void scheduleSendLocation() {
    handler.postDelayed(new Runnable() {
        public void run() {
            sendLocation();          // this method will contain your almost-finished HTTP calls
            handler.postDelayed(this, FIVE_SECONDS);
        }
    }, FIVE_SECONDS);
}

Then you just need to call scheduleSendLocation when you want to start your period calls.

like image 182
Eduardo Avatar answered Dec 19 '25 23:12

Eduardo


Ridcully is right, there is probably no reason to send the current location every 5 seconds. Here is the rational behind that:

You really only care about 2 things about the users location:

  1. Where are they right now?

  2. Have they moved since I got their first location?

So once you get a satisfactory initial location, you can just register to get callbacks whenever the users moves like this:

private LocationListener mLocationListener;

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mLocationListener = new LocationListener() {
        @Override
        public void onLocationChanged(final Location location) {
            updateLocation(location);
        }

        @Override
        public void onStatusChanged(final String provider,
                final int status, final Bundle extras) {
        }

        @Override
        public void onProviderEnabled(final String provider) {
        }

        @Override
        public void onProviderDisabled(final String provider) {
        }
    };
}

That being said, you could obviously do what these others have said and run the timer every 5 seconds. The thing is, most good locations take 10-20 seconds to run, so you might only want to run it in that interval. Also FYI, this WILL kill battery

like image 22
Jameo Avatar answered Dec 19 '25 21:12

Jameo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!