Note that there are some explanatory texts on larger screens.

plurals
  1. POWhere to Start Async Task - Android
    primarykey
    data
    text
    <p>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:</p> <p>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 (<code>SendUserActivity.sendId...</code>, 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 (<code>location.startLocation...</code>) (two databases to avoid repeating all of the user's data). </p> <p>MainActivity.java:</p> <pre><code>public void onFinish() { button.setText("SENT"); SendUserActivity.sendId(usr_id1, first, last); location.startLocation(getBaseContext(), usr_id1); </code></pre> <p>This is the second block of code from the <code>LocationActivity</code> 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. </p> <pre><code>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&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(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 } } } </code></pre> <p>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!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload