I am working in app where user location tracking all time and updated into server in background service, i update location to the server from onLocationChanged(Location location) #Override method. I want hit server when location is not changed also. is there any #Override method who is call when Gps is off?Mean,i want to notify the server that user's gps is off.
You can detect GPS on/off event/status with GpsStatus.Listener and register it with the LocationManager.
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.addGpsStatusListener(new android.location.GpsStatus.Listener()
{
public void onGpsStatusChanged(int event)
{
switch(event)
{
case GPS_EVENT_STARTED:
// do your tasks
break;
case GPS_EVENT_STOPPED:
// do your tasks
break;
}
}
});
Related
onGpsStatusChanged of GpsStatus.Listener stops getting called once you call removeUpdates on a LocationManager instance. I originally had this problem which I fixed. I'm trying to switch between gps based location tracking and network based location tracking. I've already looked into this and this. Most of the places they talk about requesting location updates from both providers simultaneously. I'm afraid this method would drain battery bigtime.
Hence I did something as follows. I had two LocationListeners: gpsListener and ntListener:
Listener mGPSListener = new GpsStatus.Listener() {
#Override
public void onGpsStatusChanged(final int event) {
switch (event) {
case GpsStatus.GPS_EVENT_STARTED:
locMgr.removeUpdates(ntListener);
locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 120000, 50, gpsListener);
break;
case GpsStatus.GPS_EVENT_STOPPED:
locMgr.removeUpdates(gpsListener);
locMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 120000, 50, ntListener);
break;
}
}
};
Now once, locMgr.removeUpdates is hit, onGpsStatusChanged stops getting called. I tried several things such as adding addGpsStatusListener after calling removeUpdates trying different gps status listeners, etc. No success yet. Am I doing something wrong?
You have to add below line before calling requestLocationUpdates on both places :
locMgr.addGpsStatusListener(ntListener);
locMgr.addGpsStatusListener(gpsListener);
I want to know if there is a way in Android (GingerBread) to know if at the moment the GPS is doing something or not. Let me be a bit more clear: I basically want to call some method or api that will tell me wheter the GPS is:
1)Fixed (GPS icon in statusbar on)
2)Searching for fix (GPS icon on statusbar blinking)
3)Inactive (No app is using location services at the moment, no icon on statusbar)
Now: I know that you can use a LocationListener to be notified of such changes BUT this is not good for me because I don't want my code to remain running waiting waiting for events, my code runs periodically at scheduled times, does something and then terminates, so I need a way to check the status of the GPS service in that precise moment, rather than wait for notifications of when it changes.
After doing lot of testing on GPS, finally I found the solution. When android app calls location manager and GPS starts searching, one event is triggered and also when gps is locked another event is triggered. Following code shows how to do this.
locationManager = (LocationManager)mContext.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (isGPSEnabled) {
if (locationManager != null) {
// Register GPSStatus listener for events
locationManager.addGpsStatusListener(mGPSStatusListener);
gpslocationListener = new LocationListener() {
public void onLocationChanged(Location loc) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES_GPS, MIN_DISTANCE_CHANGE_FOR_UPDATES_GPS,
gpslocationListener);
}
}
/*
* This is GPSListener function invoked when various events occurs like
* GPS started, GPS stopped, GPS locked
*/
public Listener mGPSStatusListener = new GpsStatus.Listener() {
public void onGpsStatusChanged(int event) {
switch(event) {
case GpsStatus.GPS_EVENT_STARTED:
Toast.makeText(mContext, "GPS_SEARCHING", Toast.LENGTH_SHORT).show();
System.out.println("TAG - GPS searching: ");
break;
case GpsStatus.GPS_EVENT_STOPPED:
System.out.println("TAG - GPS Stopped");
break;
case GpsStatus.GPS_EVENT_FIRST_FIX:
/*
* GPS_EVENT_FIRST_FIX Event is called when GPS is locked
*/
Toast.makeText(mContext, "GPS_LOCKED", Toast.LENGTH_SHORT).show();
Location gpslocation = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(gpslocation != null) {
System.out.println("GPS Info:"+gpslocation.getLatitude()+":"+gpslocation.getLongitude());
/*
* Removing the GPS status listener once GPS is locked
*/
locationManager.removeGpsStatusListener(mGPSStatusListener);
}
break;
case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
// System.out.println("TAG - GPS_EVENT_SATELLITE_STATUS");
break;
}
}
};
It is better to put GPS code in and as service to get GPS location information.
Each time if you call GPS function, GPSStatus listener is registered. GPS_SEARCHING toast comes only once when GPS is started to search and GPS_LOCKED toast displays when GPS is locked. If we call GPS function again, GPS_EVENT_FIRST_FIX event is triggered if it is locked(displays GPS_LOCKED toast) and if GPS is already started to search it won't display GPS_SEARCHING toast(i.e GPS_STARTED event won't trigger). After GPS_EVENT_FIRST_FIX event is triggered i'm removing the GPSstatus listener updates.
When GPS_EVENT_FIRST_FIX event is triggered, its better to call gpslastknownlocation() function to get fresh latest GPS fix.(Its better to look into Android developers site for more info).
I hope this will help others....
Unfortunately, there's no easy way to get the "current state" of the GPS in Android. Like others have pointed out, your best bet is to register for the onGpsStatusChanged() event and track the state.
If you are feeling adventurous, you can call call $ dumpsys location from an adb shell to get the state of the gps provider. You usually call $ adb shell from your desktop, but you can compile a native android shell app, and call an exec() from inside the shell to get the dumpsys output directly on the phone.
I want to know if a Certain Provider is Disabled will the LocationManager stop listening to the location changes for that provider ? If not then how can i manually stop listening for updates for that provider. Thanks In Advance
Your location listeners will not stop until you call myLocationManager.removeUpdates(myListener);. Check my answer to a different question [here] to know what else you need to do for connection status updates1.
give this a whirl
myLocationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE);
myListener = new LocationListener() {
public void onLocationChanged(Location location) {
Log.i("LocationListener", "Logging Change");
}
}
myLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
5000, 1, myListener);
myLocationManager.removeUpdates(myListener);
I'm planning to have a button in an activity which will start a service when clicked. However, GPS needs to be enabled for the service to do its work so I'd like to have the button disabled if GPS is disabled. Is there a way to get android to notify my activity when GPS is enabled/disabled so that I can enable/disable the button accordingly?
This link describes how to create a location listener:
http://blog.doityourselfandroid.com/2010/12/25/understanding-locationlistener-android/
I've copied the important parts down below in case the site goes down in the future. The first step is to create a LocationListener:
private final class MyLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location locFromGps) {
// called when the listener is notified with a location update from the GPS
}
#Override
public void onProviderDisabled(String provider) {
// called when the GPS provider is turned off (user turning off the GPS on the phone)
// Dim the button here!
}
#Override
public void onProviderEnabled(String provider) {
// called when the GPS provider is turned on (user turning on the GPS on the phone)
// Brighten the button here!
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// called when the status of the GPS provider changes
}
}
Then you'll want to register that listener. This should probably go in your onCreate()
LocationListener locationListener = new MyLocationListener();
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 10, this.locationListener);
The second and third parameters in requestlocationupdates you should probably make huge so you don't get locationupdates since you don't really care about those, only provider enabled/disabled changes.
Please use this code to get the GPS status. use this code in the onResume of the activity
private LocationManager mLocationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
boolean GPSprovider = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
and according to the GPSProvider you can enable and disable the button.
try this
final String GpsProvider = Settings.Secure.getString(
getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (GpsProvider.equals(""){
//No GPS
}else{
//GPS available
}
It seems for me it is getting called the first time the activity starts, just after onCreate, it then seems to be called at random intervals, whether I move or not???
Regardless of that is it simply called automatically if I have code like this in the onCreate method?
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
Is that right???
Cheers,
Mike.
Your question is not clear initially.Your code and title are not matching. I am giving answer for your title only.
You have to register Location Listener for your Location Manager, then only onLocationChanged() will be called according the settings you supplied while registering location listener.
See below code how to do that. I used GPS Provider, you can use any provider based on criteria also.
LocationManger lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, new LocationListener() {
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
});
Coming to your question, onLocationChanged() will be called if the current location update is not matching with last known location.
The updated location will be changed for every minTime (in my case 1000 milli sec) and also if device moved minDistance (in my case 0 meters) distance.
I hope you will understand this.
if you want to catch new locations, you have to register a LocationListener like this:
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
LocationListener listener = new LocationListener() {
...
}
locationManager.requestLocationUpdates(GPS_PROVIDER, intervall, distance, listener);
With intervall and distance you can configure:
If intervall is greater than 0, the LocationManager could potentially rest for intervall milliseconds between location updates
If distance is greater than 0, a location will only be broadcasted if the device moves by distance meters.
When the LocationListener is registered, the LocationManager starts to get your geo location and calls the onLocationChanged(). If the distance is very low, it can happen that the method is called very often in a short period of time. According to the intervall, the LocationManager will rest afterwards.
I think, the LocationManager will only start doing it's work, when a LocationListener is registered.
Hope that helps...
Cheers,
Tobi
public void onLocationChanged(Location location)
the above method gets called automatically once your location has been changed..