1

I need this code to be ran until it works,

LatLng current_pos = new LatLng(location.getLatitude(), location.getLongitude());
cp.SetCoordsCurrent(current_pos);

I noticed if signal is good it works fine but if signal is weak then app crashes or throws exception (with try and catch). How can I run this code until it gets coordinates? Also I need to display progressbar while it's trying to fix.

arleitiss
  • 1,304
  • 1
  • 14
  • 38

2 Answers2

2

Your question is not clear enough so not the exact answer but try this:

Move the code for getting the coordinates in AsyncTask and perform the task in doInBackground() and in preExecute show the progress dialog and in postExecute hide that progress dialog, and check if coordinates have been obtained and then set the coordinates.

Read more about AsyncTask here: http://developer.android.com/reference/android/os/AsyncTask.html

Shumail
  • 3,103
  • 4
  • 28
  • 35
  • That doesn't quite work, I have geocoder which uses AsyncTask but that's for getting actual address name, while the function I posted is getting current raw coordinates from LocationListener – arleitiss Jul 26 '14 at 23:16
1

Why don't you put it in a while loop?

while (true) {
    try {

        // If this throws any exception, then the `break` will 
        // be ignored and it jumps to `catch` clause.          
        LatLng current_pos = new LatLng(location.getLatitude(), location.getLongitude());
        cp.SetCoordsCurrent(current_pos);

        // If we get this far, then there was no exception
        // and we have the location. So we break the loop.
        Log.d("TAG", "Got the signal");
        break;

    } catch (Exception e) {
        Log.d("TAG", "Still weak signal" );
    }
}
Sam R.
  • 16,027
  • 12
  • 69
  • 122
  • That caused freeze up, and after minute or two android started responding to it and killed the activity. Perhaps there is a way to run it on background thread rather than on UI thread and while it doesn't acquire location - show progress dialog? If so how is that done? – arleitiss Jul 27 '14 at 02:22
  • 1
    @arleitiss, that's exactly what Shumail explained. Put this in `doInBackground()` method of an AsyncTask. So it's gonna run in another thread. – Sam R. Jul 27 '14 at 02:25
  • Yeah but that's the thing like, it's not tied to any AsyncTasks, it's a direct call from locationManager, the only place I use AsyncTask is for GeoCoder to get specific address. Or am I wrong to assume that AsyncTask is ONLY used on network thread (actions related to http) ? – arleitiss Jul 27 '14 at 02:27
  • @arleitiss, AsyncTask is just a simplified version of thread which facilitates everything. It work for any kind of operation that needs to be done on another thread. So you can use it. – Sam R. Jul 27 '14 at 02:30
  • Also on a side note: The bit of code I posted is being called After AsyncTask is finished. I will explain what's happening: User sets marker on map -> Marker shows infobox -> user clicks on infobox -> it opens alert dialog -> In alert dialog it loads street, city etc.. where marker was placed -> User clicks either Confirm or Cancel -> When confirm is clicked, it tries to get current user location and measure distance between marker and current location. Problem appears IF it's unable to get current location, then problems start. So I am trying to get current location until it' acquired. – arleitiss Jul 27 '14 at 02:31
  • @arleitiss, I think you need to change your code a little bit. Post some more of your code and I'll help tomorrow. (if you didn't get any help till then). – Sam R. Jul 27 '14 at 02:36
  • I will read a bit about AsyncTask in morning see if I can figure it out, if not I will update code in OP. – arleitiss Jul 27 '14 at 02:41
  • I just learned more about AsyncTask, nearly got it working except: How do I parse Location location object as parameter to AsyncTask? Or should I make it global variable and initialize it so AsyncTask accesses it rather than parsing it as param? – arleitiss Jul 27 '14 at 15:19
  • @arleitiss, I wouldn't use a global variable. Instead you might [override the constructor](http://stackoverflow.com/a/3077050/1693859) or use `Params` of the AsyncTask's generic type. See [Passing arguments to AsyncTask](http://stackoverflow.com/q/4195609/1693859) – Sam R. Jul 27 '14 at 15:28
  • Can't believe it, it finally worked + I learned a lot about AsyncTask. Thank you very much, finally now I can properly handle waiting times and show dialog boxes instead of running Handler for ever 2 seconds to launch and try do something. – arleitiss Jul 27 '14 at 15:32
  • Quick sub-question: What would be the reason my ProgressDialog doesn't get canceled onPostExecute? Is it because of while loop? – arleitiss Jul 27 '14 at 16:11
  • @arleitiss, I don't think so. `onPostExecute`, as the name implies, executes after the `doInBackground()` is done. So there is no while loop anymore in `onPostExecute`. Put some break points and debug your code to see if you reach the `ProgressDialog.cancel()` or not. – Sam R. Jul 27 '14 at 16:16
  • 1
    I realized my mistake, it wasn't able to complete while loop so it was stuck in it forever because: My LocationManager as well as Location was static, as in it was called and updated only once Activity starts, so if activity started while location was unavailable - it will stay at that. Now I modified AsyncTask params from Location to LocationManager, so it keeps updating LocationManager in while loop. Therefore once LocationManager receives Current Location = while is finished and ProgressDialog is closed. Just a simple and stupid mistake I didn't consider. – arleitiss Jul 27 '14 at 16:32