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.
Related
When I requestUpdates in the service using location listener, another listener which I have put in the activity doesn't work. The moment I stop that service (from within the app using toggle button), the activity starts listening for location again. I want both working. Why both are not working simultaneously? Am I missing something?
Try adding this to your activity :
#Override
public void onDestroy() {
super.onDestroy();
getActivity().unregisterReceiver(receiver);
}
I figured out how the requestLocationUpdates( ) function behaves.
CASE 1: [my case]
I was using two separate Inner Class implementations of LocationListener, one in the Activity and one in the Service, each requesting for location updates at the same time via requestLocationUpdates(.., .., ..,locationListener) function.
These locationListener objects were not instances of SAME class, so the one who first got used in requestLocationUpdates() function, only it was working.
CASE 2:
On the second hand, if you call requestLocationUpdates() with instance(s) of SAME LocationListener class, all of these work simultaneously.
CASE 3:
On the third hand :D, if you call requestLocationUpdates() multiple times with one same instance of a LocationListener implementation, the most recent call takes over the previous ones with whatever minTime and minDistance passed in the function.
Note: all of above cases are tested on the requestLocationUpdates(String provider, int minTime, int minDist, LocationListener listener) version of function.
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 been trying to develop Location listener using NMEANListener in android.
I have a main activity which has start button. Clicking on start button initializes the locationManager and NMEAListener.
After this I get notifications in
public void onNmeaReceived(long arg0, String arg1)
However after some time my NMEAListener is removed by android.
My Mainactivity.java class has following instances declared
private LocationManager locManager = null;
private TextView output = null;
private NMEAMessageListener nmeaMessageListener = null;
I initialize the listener in Main activity class on click of button
nmeaMessageListener = new NMEAMessageListener();
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000,100,nmeaMessageListener);
Location location=locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Please let me know as the reason for Listener being removed by android.
Is there a better way of doing it. I want to keep my location listener running for long time.
Shall i use intentservice to start NMEANListener.
Any help will be appreciated.
If you want to listen for location changes while your app is in background, then put your location listener inside Service. The fact that your listener is removed, is becaus Android is more likely to kill your app while it is in background. If you really want your service to survive low memory conditon, then add notification to is and make if foreground (call setForeground).
IntentService is more suited for single but longer tasks, it provides you with function that will perform task you want to perform in background and after it finished service will probably be finished. This makes it not appropriate for the location listener.
If location listener is implemented in Activity, then you probably remove it yourself inside onStop which is called once Activity is hidden.
Here is example SO: Background service with location listener in android
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 created an application that has a 'geolocation' feature responsible for spotting a user on the Google map like many other applicatons. I used "LocationManager.NETWORK_PROVIDER" to locate the user and at the same time I instantiate and start "MyLocationOverlay" (in the onLocationChanged() method) to get the location. Because of the second one, the GPS turns on (blinking on the top) which is OK.
The problem is, after the application is closed (back button or through task manager), the GPS feature is still hanging there, trying to get the updates.
How to turn it off after the user leaves the activity? I tried suggestions from here and other forums like putting locationManager.removeUpdates(this); and locationManager.removeUpdates(mMyLocationOverlay); within the methods onPause(), onStop(), onDestroy(). The method OnPause looks like this:
#Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
locationManager.removeUpdates(mMyLocationOverlay);
}
('this' references my class that implements LocationListener)
Please, can someone help me to turn off GPS updates after leaving the activity (it's a class that extends MapActivity) but not turn off the GPS feature on the phone itself?
Interesting thing is that when I remove the part with MyLocationOverlay, GPS will not start of course and therefore no problem. So I'm pretty sure that mMyLocationOverlay is the listener that "won't stop" and producing a problem.
googleMap.setMyLocationEnabled(false); in onPause() solved my issue. And I'm setting that to true in OnCreate(). Hope this may help others.
If you want to close (or end) the application you can use
System.exit(0);
so when the application is closed, all the services you use will close.
Set your LocationListener equal to null and re-instantiate onResume()