I want that the application should switch between GPS_PROVIDER and NETWORK_PROVIDER automatically. At present, I specify the providers in the program, so, if I specify criteria and set the locationlistener with that, then will it switch automatically to GPS_PROVIDER automatically whenever available?
For example, when the app started and listener was set at that time GPS_PROVIDER was not available or was not receiving location updates, but after some time it started receiving location updates then will the app automatically get the data from GPS_PROVIDER?
Otherwise, what is the best way of switching between the providers?
Just to add one more thing, the application will call requestLocationUpdates() only once in the app.
you can modify your onLocationChanged method like,
#Override
public void onLocationChanged(Location location) {
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
Log.e("OnLocationChange Latitude",""+location.getLatitude());
Log.e("OnLocationChange Longitude",""+location.getLongitude());
}
}
now these lat,long are given by gps provider. and for your other question I think you tested the application on emulator.
Related
I have some code that runs multiple times per second in my app. I'm trying to get my location in every cycle. I am using the following:
Location myLastPos = LocationServices.FusedLocationApi.getLastLocation(googleApiClient)
My app also runs in the background using a PARTIAL_WAKE_LOCK. With the screen on everything seems OK. When I turn the screen off my app still runs normally but I no longer get location updates consistently.
It appears that I get updates much less frequently (often minutes in between updates). I'm checking the timestamp of the location using:
myLastPos.getElapsedRealtimeNanos()
I also found that even when the screen is on I get some strange results. Sometimes I get a few milliseconds between updates, other times I get a few seconds. This is all very concerning. Can someone either help me use FusedLocationApi properly or suggest an alternative. All I really want is to poll the gps directly for lat/long a few times a second without google libraries getting in the way.
The getLastLocation() method just gets the last known location that the device happens to know. The "last known location" here means exactly that: It may not be up-to-date. Locations do come with a time stamp which could be used to asses if the location might still be relevant.
The device doesn't determine its location on its own, but only when some application request the location. So your app is now dependent on other applications requesting location updates.
If you need updates every few seconds, then request regular location updates yourself.
Android documentation recommends the FusedLocationProvider, but the LocationManager is also a perfectly valid option, if there's any reason to avoid the Google Play services.
The basic idea is to first request location updates:
// Using LocationManager as an example.
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Using GPS, requesting location updates as soon as available and even for
// the smallest changes. Here 'this' refers to our LocationListener
// implementation.
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
The updates are then received by a listener:
#Override
public void onLocationChanged(Location location) {
// We received a location update.
// Copy the value from the method parameter to our
// class member variable.
mLocation = location;
}
And when you no longer need the updates you should cancel the request:
mLocationManager.removeUpdates(this);
The approach is very similar for the FusedLocationProvider.
In my app i get location updates using location client and location request. And i implement GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener,
com.google.android.gms.location.LocationListener. And my app work like a champ.
My question is: When GPS fix a location The onLocationChanged(Location location) called. What's happen when GPS lost signal?. How i can handle this case with Location client??
The new Location Api gets the best location using the sensors(gps, wifi, etc) on your phone and you do not need to worry from which provider it is getting the location. So, you do not need to handle that. On the callback or in the pending intent, it will return the best location when you'll need according to the setInterval() & setFastestInterval() that you have specified.
This video link might help in the clear understanding of how simple the api is.
https://www.youtube.com/watch?v=Bte_GHuxUGc&spfreload=10%20Message%3A%20Unexpected%20end%20of%20input%20(url%3A%20http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DBte_GHuxUGc)
In my app I have an AlarmManager set to run an IntentService every so often to make a WebService Call to my server sending your current location
When the alarm is started I also start a Service that has a LocationManager to keep track of the location locally.
When I make the web service call to my server I use
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String provider = locationManager.getBestProvider(criteria, true);
Location lastKnownLocation = locationManager.getLastKnownLocation(provider);
since I only need the location at this point in time and the next time the web service is called again I just get it again. But it appears even though I have a service running in my background that uses the GPS and gets the right location, calling
locationManager.getLastKnownLocation(provider);
when I make the web service to get the location it gives me a stale location and not the current one. Now I would think that since I already have a locationmanager running in another service that the getLastKnownLocation would get updated to the correct location everytime the onLocationChanged gets called but that is obviously not the case which makes no sense to me.
So my question now is how can I get my correct location in my IntentService since I cannot rely on getLastKnownLocation
I do no need to know the location everytime it changes I just need to know what it is at the time I make the web service call so starting another location manager does not seem like a good idea. The only thing I can think of to solve this is to store the location from my other service in SharedPreferences and just grab whatever is in there but that seems a little hacky and i dont like hacky.
any ideas?
The last known location for android gets updated only when any app requests for a location (app can be yours or any other on the device). You have to implement the location listener and then use locationManger.requestLocationUpdates(provider, minTime, minDistance, locationListener) to ask for the location update. The android system will get the location update on your request and set the lastKnownLocation for all apps to use.
The only reason I can think of the lastKnownLocation not being set is that the android system couldn't get the location. Check that the provider requested for the location update is enabled. Also you can use Passive Provider for piggybacking on the location requests of other apps and actively look for location only if there is no update for certain time out period (This approach should be better that constantly trying to get location updates which are costly).
I have found plenty of examples of creating a location listener where you supply a particular provider, like so:
LocationManager lm =
(LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new mylocationlistener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
However, I don't need a precise location, but I'll take the best available. Is there a way to create a listener that works for the best available at the given time without creating a listener for each provider?
e.g. Use GPS if it is available, if not, Network, etc. Also, this is a widget, so I don't want to check what is available, then create a listener for that, since the widget will be up for a long duration and may live through enabling/disabling providers.
Thanks for the help
Have you considered using the PASSIVE_PROVIDER? That combined with getLastKnownLocation() might get you most of the way to what you want... You would call getLastKnownLocation() initially to establish a location if one was already known to the device, then the PASSIVE_PROVIDER would listen for updates from any location provider (i.e., location requests triggered from any other application on the device).
There would still be the possible scenario of no other application requesting location, so you'd likely want to trigger at least a single initial location fix using GPS_PROVIDER or NETWORK_PROVIDER.
Hey guys
i want to get the latitude and longitude of the users location.
I have tried developers site and stack overflow , there are loads of examples targeted on the moving user which use some thing like :-
onLocationChanged()
I dont need updates of the location , the user will just open it for a minute or two
so i need to find the location the time the application starts and what if the user has never used any geolocation app before hence i would also like an alternative for
LocationManager.getLastKnownLocation()
My code is :-
LocationManager lm = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
// connect to the GPS location service
Location loc = lm.getLastKnownLocation(lm.NETWORK_PROVIDER);
Toast.makeText(currentLocation.this, "Latitude -> "+Double.toString(loc.getLatitude())
+" Longitude is -> "+Double.toString(loc.getLongitude()), Toast.LENGTH_LONG).show();
and is working fine.
But i am scared that the getLastKnownLocation may return null when used for first time.
Thanks
Sign up for location updates and once you receive the first update, you can unregister for future updates.
In other words, create a LocationListener and register it with your LocationManager's requestLocationUpdates method. In the onLocationChanged method of your LocationListener, call the removeUpdates method on your LocationManager, with this as parameter.
It might take some time before you get a new location, but except for using getLastKnownLocation there's not really an alternative.