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.
Related
I have created a class naming MyLocationProvider with all methods provided in fusedLocationAPI as well as the following to stop the location updates:
private void stopLocationUpdates() {
mFusedLocationClient.removeLocationUpdates(mLocationCallback);
}
In main activity I use this class like this:
MyLocationProvider mylocation=new MyLocationProvider(this);
Location updates works fine but I am confused how requestLocationUpdates works. When I close then application, requestLocationUpdates is not stopped (it seem something like background process but is not listed in device services). When I open the application, a new instance of MyLocationProvider also starts working and I receive multiple parallel updates from my device.
How should I use removeLocationUpdates to stop an exact instance of location provider when they seems not to be a part of my application?
Call this method on stop or on destroy method of fragment or activity which is used by u.
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.
I have somewhat experience with Android.I would like to know how to update periodically from service to activity in Android.I am playing with an app which has Location Service running at the background and update to Activity how much the user is traveling. In the past, I broadcast distance from service and I use broadcast Receiver in Activity. However, this method is way too slow.
I recently discover that I can bind service, or use Handler to update periodically to activity from service. However, I do not understand detail especially Handler. Can anyone explain me how can I update periodically from service to activity using Handler. When I use handler, do I need to bind service? Or are those separate thing.
What I want is I want to update how much user travel by using onlocationChange() method from service to activity which has UI.
I will first explain you how the onLocationChange(Location l) method works.
This method is call-back - it means it will be called by the android system automatically. It will be called A) after amount of time passes or B ) after your position changes by certain meters. Those two parameters are specified by you when you create the LocationManager.
Having this in mind, you should for example declare a global variable with the distance, lets say
int a = 0;
Now in this method you must use the Location l which you are passed. You must add the moved distance to the variable a. Now, if all this code is inside the activity where you display the distance traveled, you simply use the variable a, else you have many choices. Either put it in shared preferences, or use function that sends that to the other activity.
Best of luck.
Have you look at the official solution?
http://developer.android.com/training/location/receive-location-updates.html
also this is a good example for what you want
https://github.com/commonsguy/cwac-locpoll
I think this can help you.
First create one Interface to Update the location
Interface to get continuous updates.
public interface ILocationlistener {
public void updateLocation(Location location);
public void updateLatitude(Double lat);
public void updateLongitude(Double lang);
}
update the ILocationlistener interface in onLocationChanged(Location location) in LocationListener so every location changes you will get the updatats.
After Use ILocationListerner interfase to update location changes in Activity or Service. After getting location use Handler or UI thread to update the UI in main thread.
For example http://www.androidsourcehelp.com/2014/03/get-location-and-its-changes-in-android.html?m=1
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.
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.