Firstly, I know LocationManager doesn't have to be called in an AsyncTask as it's already non UI blocking :)
I have an activity which
1. Gets the users current location
2. Calls a webservice to retrieve a list of specific POIs around that location.
The user can choose to view the results as map or list using a TabActivity. Bearing this in mind the AsyncTask to get users location and call to webservice is managed by the TabActivity view rather than either of the docked views.
So I want the TabActivity to start an AsyncTask which first gets the users location, then calls the webservice. A progress dialog prevents switching views using the tabs during this process.
It's pretty much all working apart from getting the users location from the AsyncTask. The webservice code is written, the mapping and overlay code is written, the task progress dialog copes with orientation changes.
The focus on location is speed rather than accuracy. If the user chooses to view results on the mapview then I will provide a 'My location' button which will enable a more accurate location to be obtained. I just want to initially get a rough location and return the search results quickly.
Maybe I'm going about this the wrong way. Maybe I should display the map view, let the map activity find the current location, then call just the webservice in the async - but then what if the user taps the list view tab during the location phase ? I was also going to allow the user to specify their default view - some people may prefer a list to a map. In this case I would have a listview which had to retrieve the location.
Thanks for any advice
Martin.
I sussed it.
The problem I was having was that the LocationListener was not being called in the AsyncTask.
The reason for this was that although I'd created and prepared a lopper in the thread I hadn't called Looper.Loop()
I start the requestLocationUpdates, kick off a timer. Either the locationmanager responds with a location, or the timer expires. At this point I call looper.quit() to ensure things return to normal.
Seems to be working. You also have to remember to respond to the back button etc, cancelling the timer and looper if the AsyncTask is cancelled.
Related
I have an android app widget that gets some information from the internet when it is first launched.
More precisely it launches a service that asynchronously does a network call. At the end of that network call, in the UI thread it updates the remoteview for the widget with new information.
Touching particular parts of the widget loads an activity which does check to see if the network call stored anything, but that is a conditional statement based on the size of the network call response, if that object doesn't contain the right things then it won't load that.
My problem is that touching the widget doesn't seem to load the activity UNTIL the asynchronous network call finishes. And this doesn't make since to me because that is a separate thread.
There never seems to be a circumstance where my activity gets to even check the condition for an empty object. Instead, I touch the widget, and that seems to be put into a queue, so when the network call finishes it then loads my activity and displays information about that network call.
Why is this happening? Is it something about how I update my remoteView? My views have listeners on them from when they are first placed on the launcher screen in the onUpdate method of the widget. Thanks
I am designing an android app where I want to list all the restaurants in a particular location. This happens on the launch of the app.
As the location changes, the list of restaurants should change accordingly even if the app is already opened.
Whatever examples I got on google do this populating of restaurants on the click of a button. But in my case it happens on the launch and if app is already launched then also depending on the location the list should change itself.
I have made a MainClass under which there is a subclass of AsyncTask and this subclass has a class which implements LocationListener.
But problem is this I have to call doInBackground() method again in onLocationChange to get the results automatically when location gets changed.
Please guide me what should I do?
Put all the logic you have for obtaining the user location and gathering new restaurant data into a Service. The Service can listen for your location updates (which hopefully are set to sparse interval if you want it running all the time) and then update the list with your AsyncTask whenever a significant enough change that requires new data occurs.
You can bind to the Service in an Activity when it's in the foreground (between onPause() and onResume() methods) to get the latest information and register as a callback if the data updates during the period while the Activity is visible to invoke notifyDataSetChanged() on your list.
You can read more about creating a Service bound locally in your application here.
HTH
Use some ArrayAdapter for the restaurants list and save it's instance in the Main class, then whenever the location changed use the AsyncTask to retreive the nearby restaurants and onPostExecute replace the ArrayAdapter internal list and call notifyDataSetChanged().
Listen to location changes in your main class. Upon receiving a (significant) change start an asynctask to query the restaurants. Pass the results (e.g. array of restaurants) to onPostExecute and update your UI accordingly.
Maybe disable updates while restaurants are being queried or cancel the query and start a new one on each new update.
I am using an AsyncTask in my application to provide the user with data when he pan or zoom the google map, I want to implement a way that cancels the current execution and execute the new request instead when the user pan/zoom so fast that he changes the location before the data is loaded, because the user will no longer need the data when he change or zoom the location on the map.
Thanks in advance
you can cancel the AsyncTask that is in execution by just calling cancel method of AsyncTask
i.e., _connectAsyncTask.cancel(true);
where _connectAsyncTask is an instance of AsyncTask.....
I am currently working on a task where i need to fetch the locations from GPS.
The concern i am having here is that once i obtain the location & the task goes completed, after this if the application goes in background i do not wish to obtain any locations until & unless my application is back on screen (foreground) whatis currently not happening. (kindly note here i am not talking about switching the activity from one to another via intent as my app has only one screen & one activity).
I wish to know what place exactly the code to handle this situation must go & how can i do achieve this. I have tried with setting the instance to null & remove update but it proves ineffective. May be i am putting it at wrong place i am putiing it outside onCreate() inside the class extending activity.
Call requestForLocationUpdates() in onResume()
and removeUpdates() in onPause()
Once I obtain the location & the task goes completed
If you just want one Lat, Lon fix: don't use the tracking mode, instead use : requestSingleUpdate()
I have implemented a LocationListener activity to get the lat/lon coordinates. I launch a MapActivity and want to get the current location and show it on the map. Now I'm confused. What is the best approach to show a progress dialog while fetching the coordinates?
Start an activity for result (LocationListener) and show a progress dialog while waiting for the coordinates to be delivered.
Implement a getCoordinates() method in the LocationListener activity and just call it once i need it (in a thread?)
Include LocationListener into MapActivity and let it continuously update field vars from onLocationChanged()
Is there any better solution? I'm pretty sure there is, actually i don't really know what I'm doing right now... :-/
A good model for displaying location(s) on a map can be found on the android official guide. Check this, and scroll down to "Helping the user decide on where to go".