Triggering LocationListener's event manually - android

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.

Related

Deregistering multiple LocationListeners

I have the following code to request location updates from both NETWORK_PROVIDER and GPS_PROVIDER.
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
mNetworkLocationListener);
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
mGpsLocationListener);
I currently have a separate listener for each, but the code in the two listeners is identical, so I want to combine them into a single listener, mLocationListener. I will pass this to both of the two requestLocationUpdates calls above, so I continue to receive updates from both sources.
I will later deregister this shared listener with
locManager.removeUpdates(mLocationListener);
The Javadoc for removeUpdates() states
Removes all location updates for the specified LocationListener.
Following this call, updates will no longer occur for this listener.
So, can I safely assume that a single call to removeUpdates() is sufficient when I have registered the same listener twice, or do I need to call removeUpdates() twice too?
Internally, the LocationManager class maps the listeners you pass to requestLocationUpdates to ListenerTransport objects, like this:
private HashMap<LocationListener,ListenerTransport> mListeners =
new HashMap<LocationListener,ListenerTransport>();
And when you call requestLocationUpdates, it puts that Listener that you passed into that HashMap..and since HashMaps do not allow duplicate keys, I don't believe you'll be able to share the same listener for different provider types.
That being said, since there cannot be duplicate keys, technically one call to removeUpdate is sufficient since the call to remove just removes the ListenerTransport value that the LocationListener maps to...but that doesn't really help you since you won't be able to have two different provider types with the same listener.

When to register for location updates?

I am writing an Android application that requires the user's current location. I register for location updates both from the network, and from GPS using the following code (locationManager is already defined):
// Register the listener with the Location Manager to receive location updates.
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
getResources().getInteger(R.integer.gps_min_time),
getResources().getInteger(R.integer.gps_min_dist), this);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
getResources().getInteger(R.integer.gps_min_time),
getResources().getInteger(R.integer.gps_min_dist), this);
I currently have this code in onCreate, but in order to save power, I remove both listeners in onPause and add them both again in onResume.
When the application starts, it adds both listeners twice, once in onCreate, and once in onResume. I have two question about this:
Does having each listener added twice mean that it actually gets added twice, or does the second call have no effect?
Should I remove the requestLocationUpdates from onCreate and just have them in onResume or should I remove all listeners first in onResume before adding them again?
your Listener will be added as a key to a Hashmap. So If you insert this (Your Activity) and don't override equals it should be overridden, and has no effect, because it is the SAME Listener.
Anyway, I would remove the register inside onCreate and just leave it inside onResume, as you will save at least the time to Hash your listener and the Method calls.
I would just add them in onResume(). Create the manager in onCreate() and add and remove listeners in onResume() and onPause().
I don't have an answer to your first question.

Stop Location Updates after first time

I am using a service which implements LocationListener class.Now i want to know the current location(Latitude,Longitude) of the device. I have used requestLocationUpdates method so that onLocationChanged method gets called when the location changes. I want that onLocationChanged method should be called only once i.e. when i get the location for the first time, it should not be called again.
I have called stopSelf method to stop the service inside onLocationChanged method. But still i am getting location updates.
Please help me.
Call removeUpdates on LocationManager, passing your location listener.
mLocationManager.removeUpdates(mListener);

onLocationChanged call the onDraw method in my custom view

I have a MapImageViewer Activity class that has methods for checking the phones gps location. This activity then calls a custom view of mine called MapCanvas - where the constructor takes the latitude and longitude and then draws a circle to the corresponding pixels on an image of a map.
This works ok, but I am wondering how can I update and call the onDraw method every time the gps coordinates change? I know it needs to go in the onLocationChanged method..but i'm not sure how i can pass the new latitude and longitude values from there to my Custom View class.
What about sending lat/lon from the onLocationChanged() method by using .sendBroadcast() and handle them inside your View class with a BroadcastReceiver?
Alternatively you could use a callback method from the LocationListener like shown in this post.
Call the view's invalidate() method every time you get new coordinates (or postInvalidate() if the locationListener is in a different thread from the UI thread)

doubt in using Location Listener

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.

Categories

Resources