i'm using it to recover the location of the user :
locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,5000,1000, this);
Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
The problem is, the location seems to be always enabled when me i just want to use it ONE time.
How can i stop the location (when the users "pause" the application etc) ?
I tried it, in my function "onPause" :
locManager.removeUpdates(this);
locManager = null;
But it's not running.
You need to call
super.onPause()
So the onPause() function would look like:
public void onPause(){
locManager.removeUpdates(locListener);
super.onPause();
}
If you have not set a locationListener and getting a NullPointerException, wrap the removeUpdates in a null check:
if (locManager != null)
locManager.removeUpdates(this);
Related
I created an app with an GoogleMapView. In order to reduce battery consumption, I want to stop the location requests, when the app is not opened. I found that this can be done with the LocationManager. For example like this:
#Override
public void onPause() {
locationManager.removeUpdates(this);
super.onPause();
}
#Override
public void onResume() {
locationManager.requestLocationUpdates(locationManager.getBestProvider(criteria, true), minUpdateTime, minUpdateDistance, this, null);
super.onResume();
}
But at the moment I am not using the LocationManager at all and the GoogleMapsView still manages to get the current location. I merely need to call mMap.setMyLocationEnabled(true); in my OnMapReadyCallback() function. So it seems like the GoogleMapsView implements its own sort of LocationManager which leads to the question, if it is necessary to create a LocationManager just to tell it to stop updating when the app is closed, or is it enough to call mMap.setMyLocationEnabled(false); in my onPause() function?
I've got this problem I don't see anything wrong in the code but getLastKnownLocation returns null every time . any ideas ?
public class LocationDemo2Activity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText et1 = (EditText) findViewById(R.id.editText1);
LocationManager manager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null) et1.setText((int)location.getLatitude());
else et1.setText("null");
}
}
thanks
getLastKnownLocation() will frequently return null, particularly if the location provider (e.g., GPS) has not been used recently. You only use getLastKnownLocation() in situations where you either do not really need a location (but would like to have one) or where you will use other techniques if getLastKnownLocation() returns null (e.g., request location updates).
Created a Location alarm based app. Everything is working fine on emulator. But the following error occurs in real device.
Scenario:
Turned on GPS.
Opened my app in phone.
No GPS symbol in status bar.
App getting force closed.
To fix this scenario, I am doing the following thing
Open any application like Maps, GPS Essentials.
My GPS values is getting fixed.
Opening my application after that.
App working fine.
Providing the necessary codes below:
in my Oncreate method
fixlocation = orgManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
#Override
protected void onResume() {
super.onResume();
orgManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1000, this);
orgManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 1000, this);
mo.enableCompass();
mo.enableMyLocation();
}
/** Stop the updates when Activity is paused */
#Override
protected void onPause() {
super.onPause();
orgManager.removeUpdates(this);
mo.disableCompass();
mo.disableMyLocation();
}
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
//orgManager.removeUpdates(this);
if (location != null) {
currLatitude = location.getLatitude();
currLongtitude = location.getLongitude();
}}
What do I need to do now?
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
currLat = location.getLatitude();
currentLang = location.getLongitude();
Try this to get current location.
onLocationChanged will call only when location of your device changes but
first time when your application starts you can use this code.
I am using this piece of code to stop gps locating the problem is that sometimes android kills my process starting it again... and the handle becomes invalid...making it impossible to stop the gps...how can i do this ?
here is the code:
lm.removeUpdates(locationListener);
locationListener = null;
Try-
locationManager.removeUpdates(locListener);
if (locListener != null) {
locListener = null;
}
if (locationManager != null) {
locationManager = null;
}
And make sure you initialize your handler in onResume() and not onCreate()
Also put the above piece of code in onLocationChanged() since, as you've got the location, you don't need gps (for quite some time at least) as you said.
I am struggling a bit with the LocationListener in Android. I want to make an app that will get the current GPS location, and then afterwards sleep for a long time. A day or more. In this period of time i want the GPS notification icon to not show.
What i have now, is in the onLocationChanged a Thread.sleep(x) but this will keep the icon on in the sleep period. How can i do this, and is there a better approach than to use Thread.sleep?
Thanks in advance
You have to turn off the LocationManager completly for that. I did it in my App, where I only check the Location every 10 seconds, 'cause I found out, that the turning off saves a bit more battery power than a min_distance or min_time of the LocationManager.
Something like:
// Turning off
mLocationManager.removeUpdates(gpsListener);
mLocationManager = null;
// Turning on again
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, GPS_MINTIME, GPS_MINDISTANCE, gpsListener);
The icon will disappear till the LocationManager is turned on again.
If you have overlays, think to disable the location on them too :
#Override
protected void onResume()
{
Log.i("ProjetTEA", "onResumeMain");
if (mLocationListener != null)
{
mLocationOverlay.enableMyLocation();
mLocationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(mBestProvider, 10000, 2, mLocationListener);
}
super.onResume();
}
#Override
protected void onPause()
{
Log.i("ProjetTEA", "onPauseMain");
mLocationOverlay.disableMyLocation();
mLocationManager.removeUpdates(mLocationListener);
mLocationManager = null;
super.onPause();
}