//update last best location bestLocation = getLocation(context);
//if location is not ready or don`t exists, try again if(bestLocation == null && gps_enabled){ System.out.println("BestLocation not ready, continue to wait"); handler.postDelayed(this, iteration_timeout_step); }else{ //if best location is known, calculate if we need to continue to look for better location //if gps is enabled and min satellites count has not been connected or min check count is smaller then 4 (2 sec) if(stop == false && !needToStop()){ System.out.println("Connected " + sat_count + " sattelites. continue waiting.."); handler.postDelayed(this, iteration_timeout_step); }else{ System.out.println("#########################################"); System.out.println("BestLocation finded return result to main. sat_count=" + sat_count); System.out.println("#########################################");
// removing all updates and listeners myLocationManager.removeUpdates(gpsLocationListener); myLocationManager.removeUpdates(networkLocationListener); myLocationManager.removeGpsStatusListener(gpsStatusListener); sat_count = 0;
// send best location to locationResult locationResult.gotLocation(bestLocation); } } } };
/** * Determine if continue to try to find best location */ private Boolean needToStop(){
// starting best location finder loop handler.postDelayed(showTime, iteration_timeout_step); }
/** * GpsStatus listener. OnChainged counts connected satellites count. */ public final GpsStatus.Listener gpsStatusListener = new GpsStatus.Listener() { public void onGpsStatusChanged(int event) {
if(event == GpsStatus.GPS_EVENT_SATELLITE_STATUS){ try { // Check number of satellites in list to determine fix state GpsStatus status = myLocationManager.getGpsStatus(null); Iterable<GpsSatellite>satellites = status.getSatellites();
/** * Gps location listener. */ public final LocationListener gpsLocationListener = new LocationListener(){ public void onLocationChanged(Location location){
} public void onProviderDisabled(String provider){} public void onProviderEnabled(String provider){} public void onStatusChanged(String provider, int status, Bundle extras){} };
/** * Network location listener. */ public final LocationListener networkLocationListener = new LocationListener(){ public void onLocationChanged(Location location){
} public void onProviderDisabled(String provider){} public void onProviderEnabled(String provider){} public void onStatusChanged(String provider, int status, Bundle extras){} };
/** * Returns best location using LocationManager.getBestProvider() * * @param context * @return Location|null */ public static Location getLocation(Context context){ System.out.println("getLocation()");
// fetch last known location and update it try { LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);