title: How can I invoke a method every 5 seconds in android?
body: <p>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.</p>
  
  <p>The method which it must invoke every 5 seconds:</p>
  
  <pre><code>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"+"&amp;long="+longitude+"&amp;lat="+latitude+"&amp;alt="+altitiude+"&amp;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
          }
  
  
  
  }
  </code></pre>