I have just started working on my first Android application and am going ok. I have managed to get the app to locate my current position on a map and place the blue circle there. I can see the satellite icon in the notification bar working (satellite dish with rays coming off it).
I am very new to android phones altogether but upon being done with my app I just exit it by either using the back key or the home key which works fine, however I notice the satellite icon is still working. Going back to my phone an hour later the GPS is still running. This of course sends the phone battery flat very quickly.
I assume unlike iPhone that Android apps can run in the background and this is still running. How do I get my app to stop using GPS when it is no longer on the map view?
Here is an example of what I have done so far:
//find and initialise map view
private void initMapView() {
map = (MapView) findViewById(R.id.map);
controller = map.getController();
map.setSatellite(false);
map.setBuiltInZoomControls(true);
}
//start tracking the position on the map
private void initMyLocation() {
final MyLocationOverlay overlay = new MyLocationOverlay(this, map);
overlay.enableMyLocation();
//overlay.enableCompass(); does not work in emulator
overlay.runOnFirstFix(new Runnable() {
public void run() {
//zoom in to current location
controller.setZoom(8);
controller.animateTo(overlay.getMyLocation());
}
});
map.getOverlays().add(overlay);
}
Do I need to disable something when I leave the map view? if so, how?
Cheers guys
You should probably take a look at the Activity Lifecycle. In short, your assumption is correct: Android does not just kill your App when you leave it.
You'll have to implement the onPause() method and unregister the GPS listener. And maybe also remove the mapview, but I don't think that matters much in terms of battery life.
Also, you should move the registration of the GPS into the onResume() method. That's necessary in order to enable the GPS again when your App coms back into view.
The onPause(), onResume(), onCreate() (and so on) methods get called by android itself at the appropriate times.
Daniel,
Within the maps overlay API there are function calls that will register and unregister the listener for you...
You are already calling the enableMyLocation(); now you need to call overlay.disableMyLocation();
In the onPause.
Related
I want to update the value of a marker when the maps stop moving, but I canĀ“t detect it. Does someone know how?. I already tried with the PanGestureRecognizer
Implement the GoogleMap.IOnCameraIdleListener and assign it to your map instance (assumably in the MapReady callback):
Implement GoogleMap.IOnCameraIdleListener
public void OnCameraIdle()
{
// a callback that is invoked when camera movement has ended.
}
Assign the Idle listener to the GoogleMap instance:
googleMap.SetOnCameraIdleListener(this);
Re: setOnCameraIdleListener
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 realise I can use a GoogleMap.OnMyLocationButtonClickListener to determine when the user has pressed the button to make the GoogleMap follow their location on screen, but is there a way I can receive an event for when the user is no longer following the device? For example, if the user has moved the map, which stops the map from locking to the user's location.
You could add a OnCameraChangeListener, it will get called if the map is dragged. In its onCameraChange compare the CameraPosition#target LatLng with MyLocation.
Here are the camera change and my location listeners in the same API:
https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/GoogleMap.OnCameraChangeListener
https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/GoogleMap.OnMyLocationChangeListener
I suggest using a View overlapping MapFragment and overriding its onTouchEvent, checking DOWN event to stop following position. This is when user starts any interaction with map.
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()
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()