doubt in using Location Listener - android

I have used LastKnownLoaction at the start-up Activity of my app to get the location ,if it returns null then i register for a Location Listener for both gps and network providers.After requesting for Listener to listen for any location change i switch over to main Activity page using startActivity.Further in the startup Actyivity's onLocation in both Listener i am trying to get location and if any values found then i try to store them in preference and remove updated from both listener and notify the user about the location being updated.
Is this logic is correct way of coding ,do the listener listen for location update when its no longer on the top of the stack ,hence i am using a emulator i couldn't test gps functionality,i required your suggestions on this
thanks

You shouldn't register a LocationListener only if getLastKnownLocation() returns null, because that means the particular location provider is disabled. Also caching a real-time value like location doesn't sound like a good design. I would suggest to register/unregister LocationListener for each Activity in it's onStart/onStop methods.

Related

Android MapView keeps Location on

I have a simple fragment with a mapview displayed. I have override all the necessary methods according to the documentation (destroy, resume and start) to cal the same method of the mapview. However when I detach the fragment or exit the application the location keeps on. It will drain too much battery. Is there any procedure I can make to stop the location feature?. I've tried calling mapview.onDestroy, mapview=null, etc etc and the location keeps on.
Thanks
Check How to unregister Listener & stop service from within broadcastreceiver. If you're using LocationManager to get location updates, call removeUpdates(getActivity()) on your LocationManager instance to stop them.
Edit as requested:
Searching the current location via GPS is disabled by calling setMyLocationEnabled(false) on the GoogleMap instance.

Android: get location updates without interfering with osmdroid.MyLocationOverlay

Android / location:
I would like to receive location updates every 5 seconds.
Should I call the method:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,5000, 0, gpsLocListener)?
It is important for me NOT to interfere with the osmdroid MyLocationManager functionality.
Or should I use the MyLocationOverlay.onLocationChanged(), e.g. by inheritance of the MyLocationOverlay class.
If so, how often will it be called?
There should be no problem having multiple listeners on the location manager. Each one will get events separately. In fact, if you wanted you could use a new instance of osmdroid's GpsMyLocationProvider to feed you location updates. Call mGpsMyLocationProvider.startLocationProvider(myLocationConsumer) where myLocationConsumer is an instance of IMyLocationConsumer that receives and handles gps updates as you wish. You can call mGpsMyLocationProvider.setLocationUpdateMinTime() to adjust how often you get updates.

As location changes Restaurant list should change automatically without a button click app

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.

Android LocationManager in AsyncTask

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.

Triggering LocationListener's event manually

Is there a way to manually trigger the code inside a LocationListener's onLocationChanged method? It should receive a Location object with the last known location, I guess.
Sure thing, if it's your LocationListener - onLocationChanged() is a public method; just call it with whatever Location object you have on hand (maybe have your location listener cache it somewhere, or use LocationManager's getLastKnownLocation method to grab the last location seen from a specific provider and call onLocationChanged() with that).
You can use the setTestProviderLocation method of the LocationManager to mock new locations and have the onLocationChanged method of the registered listeners called when you want.
You should check the reference page. You also have an example here.

Categories

Resources