0

I have tried to get this code sorted couple of times. Here’s the scenario I have tried so far…

Scenario: Once the activity is started I want to get the coordinates (long & lat) based on network/gps provider. This should run in background and it should keep checking until long & lat is not null or “0.0”. Thus, I have tried the following code with AsyncTask and thereby using LocationListener in doInBackground method.

Source Code:

public class GetLocation extends AsyncTask<String, String, String>
{  
    private myTest test;
    boolean running =true;
    private Context cont;
    String addressString;

            public GetLocation(myTest fr, Context contxt)
            {
                test = fr;
                cont = contxt;
            }

            @Override
            protected void onPreExecute()
            {  
                super.onPreExecute();                   

            } 

            @Override 
            protected void onProgressUpdate(String... values) {
                super.onProgressUpdate(values);

             }

            @Override 
            protected void onPostExecute(String result)
            {   
                test.GetContent();
            }

            @Override
            protected String doInBackground(String... params) {
                Looper.myLooper().prepare();

                LocationManager locationManager;
                locationManager = (LocationManager) cont
                        .getSystemService(Context.LOCATION_SERVICE);

                Criteria crta = new Criteria();
                crta.setAccuracy(Criteria.ACCURACY_FINE);
                crta.setAltitudeRequired(false);
                crta.setBearingRequired(false);
                crta.setCostAllowed(true);
                crta.setPowerRequirement(Criteria.POWER_LOW);
                String provider = locationManager.getBestProvider(crta, true);

                Location location = locationManager.getLastKnownLocation(provider);
                updateWithNewLocation(location);
                LocationListener locationListener = new LocationListener() {

                    @Override
                    public void onLocationChanged(Location location) {
                        updateWithNewLocation(location);
                    }

                    @Override
                    public void onProviderDisabled(String provider) {
                        updateWithNewLocation(null);
                    }

                    @Override
                    public void onProviderEnabled(String provider) {
                    }

                    @Override
                    public void onStatusChanged(String provider, int status,
                            Bundle extras) {
                    }

                };

                locationManager.requestLocationUpdates(provider, 0, 0,
                        locationListener);
                Looper.loop();

                return addressString;
            }

            private void updateWithNewLocation(Location location) { 

                Constants.lat = Double.toString(location.getLatitude());
            Constants.long = Double.toString(location.getLongitude());        

          }
 }

Problem: However, I also know that Looper’s can help to keep the thread active. I want to be able to get valid coordinates and this should loop until its received. How do i call to get the locations over and over again until valid one's are received? (I could put conditions within the loops i have provided but im not aware as to how and what methods i should call to achieve this). Please provide a code snippet if possible.

Cheers!

Pavan Welihinda
  • 605
  • 3
  • 11
  • 26
  • Please check with the below answer. http://stackoverflow.com/questions/7709030/get-gps-location-in-a-broadcast-receiver-or-service-to-broadcast-receiver-data-t/7709140#7709140 – itsrajesh4uguys Jun 01 '13 at 09:38
  • I tried that code. I managed to ger the service running but i dont see the ReceiverPositioningAlarm is getting hit in any case when i debug. – Pavan Welihinda Jun 02 '13 at 07:19

2 Answers2

0

You have implemented this wrong. There is no need to create the AsyncTask. The LocationManager will asynchronously fetch locations for you and deliver them on your LocationListener.onLocationChanged. Once you get a proper Location you can call LocationManager.removeUpdates() and this will stop for further delivery of Locations.

Ovidiu Latcu
  • 71,607
  • 15
  • 76
  • 84
  • Yes but i want to wait until proper locations are received. So should i put a call back function from onLocationChanged method? – Pavan Welihinda Jun 02 '13 at 05:06
0

Do not use AsyncTask for this. You just registered the LocationManager that is listen by LocationListener and use progressbar for that. Once call LocationListner.onLocationChanged try to get Lat and Long and dismiss the progress bar. OnLocationChanged will be call at you move your device for a short distance.

Sunil Kumar
  • 7,086
  • 4
  • 32
  • 50
  • So i should dismiss the progress bar in onlocationchanged method is it? because i somehow needs to keep on checking whether proper coordinates are received. – Pavan Welihinda Jun 02 '13 at 05:07