Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LocationHelper
- {
- public interface RefreshInterface
- {
- public void preExecute();
- public boolean isLocationValid(Location location);
- public void postExecute(Location location);
- }
- private static class RefreshLocation implements LocationListener
- {
- private long timeout = 0; // 20 vterin
- private long startTime = 0;
- private boolean finished = false;
- private Handler handler = new Handler();
- private Runnable r = null;
- private RefreshInterface iface = null;
- private RefreshLocation(RefreshInterface iface, long timeout)
- {
- this.startTime = System.currentTimeMillis();
- this.iface = iface;
- this.timeout = timeout;
- }
- private void refresh()
- {
- if(this.iface instanceof RefreshInterface)
- {
- this.iface.preExecute();
- }
- r = new Runnable() {
- @Override
- public void run()
- {
- if(!finished)
- {
- finished = true;
- getLocationManager().removeUpdates(RefreshLocation.this);
- if(iface instanceof RefreshInterface)
- {
- iface.postExecute(null);
- }
- }
- }
- };
- String bp = getBestProvider();
- Location lp = getLocationManager().getLastKnownLocation(bp);
- if(lp instanceof Location && lp.getTime() + 2*60*1000 > System.currentTimeMillis())
- {
- if(iface instanceof RefreshInterface && iface.isLocationValid(lp))
- {
- iface.postExecute(lp);
- return;
- }
- }
- if(getLocationManager().isProviderEnabled(bp))
- {
- if (getLocationManager().isProviderEnabled(LocationManager.GPS_PROVIDER)) {
- getLocationManager().requestSingleUpdate(LocationManager.GPS_PROVIDER, this, null);
- }
- if (getLocationManager().isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
- getLocationManager().requestSingleUpdate(LocationManager.NETWORK_PROVIDER, this, null);
- }
- finished = false;
- handler.postDelayed(r, timeout);
- }
- else
- {
- if(iface instanceof RefreshInterface)
- {
- iface.postExecute(null);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment