Note that there are some explanatory texts on larger screens.

plurals
  1. POLocationListener InSide AsyncTask
    text
    copied!<p>Hi I am New to android programming and currently developing an application that uses location manager to get user location and place a marker on a map. i am attempting to use AsyncTask to run the LocationListener and Constantly update the marker when the user location has changed. this is the class i am working on...</p> <pre><code>public class IncidentActivity extends MapActivity{ public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); this.setContentView(R.layout.incidentactivity); mapView = (MapView)findViewById(R.id.mapView); mapView.setBuiltInZoomControls(true); mapView.setTraffic(true); mapController = mapView.getController(); String coordinates[] = {"-26.167004","27.965505"}; double lat = Double.parseDouble(coordinates[0]); double lng = Double.parseDouble(coordinates[1]); geoPoint = new GeoPoint((int)(lat*1E6), (int)(lng*1E6)); mapController.animateTo(geoPoint); mapController.setZoom(16); mapView.invalidate(); new MyLocationAsyncTask().execute(); } private class MyLocationAsyncTask extends AsyncTask&lt;Void, Location, Void&gt; implements LocationListener{ private double latLocation; private Location l; //location management variables to track and maintain user location protected LocationManager locationManager; protected LocationListener locationListener; @Override protected Void doInBackground(Void... arg0) { Looper.prepare(); locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1, locationListener); this.publishProgress(l); return null; } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); } @Override protected void onProgressUpdate(Location... values) { super.onProgressUpdate(values); } //this method is never executed i dont know why...? public void onLocationChanged(Location location) { if (location != null){ latLocation = location.getLatitude(); Toast.makeText(getBaseContext(), " Your latLocation :" + latLocation, Toast.LENGTH_LONG).show(); //Log.d("Your Location", ""+latLocation); } } public void onProviderDisabled(String provider) { } public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } } } </code></pre>
 

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