Tutorial :I have one Activity. In this Activity, I executed a AsyncTask , but it's not working



Question:

private class ExecuteLocations extends AsyncTask<String, Void, Void>{      private final ProgressDialog dialog = new ProgressDialog(ListProfiles.this);        protected void onPreExecute() {          //this.dialog.setMessage("Starting pre-execute...");          //this.dialog.show();      }        @Override      protected Void doInBackground(String... arg0) {          check_profiles_lm=(LocationManager) ListProfiles.this.getSystemService(LOCATION_SERVICE);            myLocListen = new LocationListener(){              @Override              public void onLocationChanged(Location location) {                  HashMap params = new HashMap();                  params.put("lat", Double.toString(location.getLatitude()));                  params.put("long", Double.toString(location.getLongitude()));                  postData("http://mydomain.com",params);                }              @Override              public void onStatusChanged(String provider, int status,Bundle extras) {               }              @Override              public void onProviderDisabled(String provider) {               }              @Override              public void onProviderEnabled(String provider) {              }          };                check_profiles_lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 30000, 0, myLocListen);          return null;      }        protected void  onPostExecute(final Void unused) {          if (this.dialog.isShowing()) {              //this.dialog.dismiss();          }          //Do something else here       }    }  

Basically, my objective is:

  1. Constantly post the latitude/longitude to my website every minute or so.
  2. Of course, I want to do this in another thread, so it doesn't mess up my UI thread. This AsyncTask is supposed to solve that...but it's bringing up an error and I don't know why.

What can you do to accomplish my objective? It's very simple...just scan location and post to web every minute, in the background.

By the way, my onCreate method is like this. That's basically it.

public void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);              new ExecuteLocations().execute();              setContentView(R.layout.main_list);      }  


Solution:1

Break it into 3 steps:

  1. Make whatever you want to work work w/o AsyncTask
  2. Make sure you've figured out AsyncTask, do a simple Log.e("haha", "gotcha") in doInBackground(...) and check that it shows
  3. Combine the two.

For this case, I'd probably go with a Service triggered by AlarmManager. Guess you'll need the latter anyways.


Solution:2

Create this as a Service. No need to have a Timer or AlarmManager as you register for location events and act appropriately.

An example of listening to location events can be found at http://code.google.com/p/android-bluetooth-on-motion/source/browse/#svn/trunk/BluetoothOnMotion/src/com/elsewhat

        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_NETWORK, MIN_DISTANCE_NETWORK,locationListener);          locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_GPS, MIN_DISTANCE_GPS,locationListener);  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »