I'm still learning Java/Android development and I need some guidance to implement an Async task. I'm mostly unsure of where to start it in my program. I have outlined some code below, could someone show me where and how to implement the task? My app sends GPS coordinates to a database every second and I need a separate task so I don't bog down the main thread and risk an ARN error. Here is my code:
This first block is from my main activity. When the button is pressed, a three second countdown is initiated, and when it finishes (onFinish) the user data is sent to one database (SendUserActivity.sendId...
, this is actually a class, not an activity, just poorly named) and the app then begins to track the user and send the coordinates to a different database every second (location.startLocation...
) (two databases to avoid repeating all of the user's data).
MainActivity.java:
public void onFinish() {
button.setText("SENT");
SendUserActivity.sendId(usr_id1, first, last);
location.startLocation(getBaseContext(), usr_id1);
This is the second block of code from the LocationActivity
class (also a class and not an activity, poor naming again). It requests a location update every second and then posts it to a PHP script that inserts it in a MySQL database.
public class LocationActivity {
private LocationManager locManager;
private LocationListener locListener;
public void startLocation(Context context, String usr_id2)
{
final String usr = usr_id2;
//get a reference to the LocationManager
locManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
//checked to receive updates from the position
locListener = new LocationListener() {
public void onLocationChanged(Location location) {
send(location, usr);
}
public void onProviderDisabled(String provider){
//labelState.setText("Provider OFF");
}
public void onProviderEnabled(String provider){
//labelState.setText("Provider ON ");
}
public void onStatusChanged(String provider, int status, Bundle extras){
//Log.i("", "Provider Status: " + status);
}
};
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locListener);
}
public void send(Location loc, String usr_id2) {
Log.i("", String.valueOf(loc.getLatitude() + " - " + String.valueOf(loc.getLongitude())));
String lat = String.valueOf(loc.getLatitude());
String lon = String.valueOf(loc.getLongitude());
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://example.com/test/example.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("lat", lat));
nameValuePairs.add(new BasicNameValuePair("lon", lon));
nameValuePairs.add(new BasicNameValuePair("id", usr_id2));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
}
catch (ClientProtocolException e) {
// TODO Auto-generated catch block
}
catch (IOException e) {
// TODO Auto-generated catch block
}
}
}
To reiterate my question, how would I implement an async task to send the GPS information to the database? I have read about IntentServices, but some have suggested that I use an async task. Either way, let me know where and how to implement one of these; I'd really appreciate it. Thanks!