Switching between the networks in android - android

Hey guys
I am developing an application in which i need to find the latitude
longitude of the user. I am able to find latitude longitude by network providers through following code :-
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
loc = lm.getLastKnownLocation(lm.NETWORK_PROVIDER);
Toast.makeText(BaseScreen.this, "Lat: "+loc.getLatitude()+" long: "+loc.getLongitude(), Toast.LENGTH_LONG).show();
Now the following are the cases which i need to implement :-
What if user is in a building and he can not retrieve data from GPS
What if the user has GPS switched off and wants to get location from Network
My application has like 20 activities if i place the code of locationlistener in one base screen which is extended by other activities and keep saving the latitude and longitude in some datamanager then will the call back work in that base screen activity even if the activity is not the main UI activity??

a. Usually we should register for both GPS and network provider while requesting updates, this takes care of the line-of-sight problem where we automatically fall back on network provided values.
b. If the GPS is switched off you can check that from the code by using isProviderEnabled(), if not than your only option is to use the Network Provider.
c. Better still have LocationListener as a separate class and in onLocationChanged store the location received which should be available across your application.
To register use android.location.LocationListener.requestLocationUpdates

Related

Get location updates using location client and location request with GPS only

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)

getLastKnownLocation is not the actual location

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).

Does specifying criteria switch the location provider in Android?

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.

How to get current location on android

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.

Search for Best Provider in Android Application

I am trying to search for best provider with this case below:
// GPS
case R.id.main_menu_gps:
// Set up location services
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
mBestProvider = mLocationManager.getBestProvider(new Criteria(), true);
Log.d(DEB_TAG, "####Value of mBestProvider is " + mBestProvider);
if(mBestProvider != null){
showGpsSearchingDialog();
mLocationManager.requestLocationUpdates(mBestProvider, 150000, 1, this);
} else {
Log.d(DEB_TAG, "Provider is null");
showGpsAlertDialog();
}
break;
My device is returning "GPS" as the best provider but is not able to find a location and my progress dialogue is displayed forever searching. If I go into the phone settings of "Location/Security" and check the "Use wireless networks" the best provider is Network and it works to return a location.
Am I doing something wrong when the best provider is GPS and no data is returned?
You may take a look at my strategy to choose best provider What is the simplest and most robust way to get the user's current location on Android?
If GPS is enabled it will always return GPS as the best provider since you specified an empty criteria. It can take a decently delay for a GPS fix and if you are in a building you may never get a fix at all. So if the network location will be acceptable then you should just do network. Or have a timeout so that at some point you stop waiting on gps and then switch to network instead. If you are using a MapView, you may want to use the MyLocationOverlay because it's internal logic will handle that for you.
Your code looks fine btw.
You aren't really doing anything wrong, there are just certain techniques for gaining the location via GPS. The main issue is that the GPS location may be the best available provider (in terms of accuracy) however it may not be able to obtain the current location do to network/structural obstruction. Without logic in place to determine the amount of time that has gone by while the provider has attempted to attain the location unsuccessfully or without the accuracy required; you may never get the location via GPS and will have to use a different provider or the last known location as a fall-back.

Categories

Resources