Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LocationListener extends ILocationListener
- {
- private final static int TIME_OUT = 100 * 1000;
- private final static long HALF_MINUTE = 30 * 1000;
- private static LocationListener listenerWithBackgrounProcess;
- private Location mLocation;
- private final LocationManager manager;
- private FindLocation mFindLocation;
- LocationListener mLocationListener = new LocationListenerImpl();
- private LocationListener(final Context context)
- {
- log("constructor");
- manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
- }
- public static LocationListener getInstance(final Context context)
- {
- if (listenerWithBackgrounProcess == null)
- listenerWithBackgrounProcess = new LocationListener(context);
- return listenerWithBackgrounProcess;
- }
- private void startFind()
- {
- log("startFind");
- initLoacationILstener();
- if (mFindLocation != null && !mFindLocation.isCancelled())
- mFindLocation.cancel(true);
- mFindLocation = new FindLocation();
- mFindLocation.execute();
- }
- private boolean hasLastKnownLocation()
- {
- log("check hasLastKnownLocation");
- Location bestLastKnown = null;
- List<String> providers = manager.getAllProviders();
- int i = 0;
- for (; i < providers.size(); i++)
- {
- Location temp = manager.getLastKnownLocation(providers.get(i));
- if (temp != null)
- {
- bestLastKnown = temp;
- break;
- }
- }
- if (bestLastKnown != null)
- {
- for (int j = i; j < providers.size(); j++)
- {
- Location temp = manager.getLastKnownLocation(providers.get(j));
- if (temp != null && temp.getTime() < bestLastKnown.getTime())
- bestLastKnown = temp;
- }
- if (bestLastKnown.getTime() <= HALF_MINUTE)
- {
- log("hasLastKnownLocation || time:" + String.valueOf(System.currentTimeMillis() - bestLastKnown.getTime()));
- if (locationRunnable != null)
- locationRunnable.locationUpdate(bestLastKnown);
- return true;
- }
- }
- return false;
- }
- private Location endFind()
- {
- long sec = System.currentTimeMillis();
- //time out 20 sec
- while (this.mLocation == null && System.currentTimeMillis() - sec < TIME_OUT)
- {}
- log("endFind");
- return this.mLocation;
- }
- @Override
- public void enableMyLocation()
- {
- mLocation = null;
- log("enableMyLocation");
- if (!hasLastKnownLocation())
- startFind();
- }
- @Override
- public void disableMyLocation()
- {
- log("disableMyLocation");
- if (mFindLocation != null && !mFindLocation.isCancelled())
- mFindLocation.cancel(true);
- manager.removeUpdates(mLocationListener);
- }
- private void initLoacationILstener()
- {
- List<String> providers = manager.getProviders(true);
- for (String provider : providers)
- {
- if (manager.isProviderEnabled(provider))
- {
- manager.requestLocationUpdates(provider, 0, 0, mLocationListener);
- log("init providers || " + provider);
- }
- }
- }
- private class LocationListenerImpl implements LocationListener
- {
- private LocationListenerImpl()
- {
- }
- @Override
- public void onLocationChanged(Location location)
- {
- log("onLocationChanged");
- LocationListenerWithBackgroundProcess.this.mLocation = location;
- if (locationRunnable != null)
- locationRunnable.locationUpdate(location);
- disableMyLocation();
- }
- @Override
- public void onProviderDisabled(String provider)
- {
- }
- @Override
- public void onProviderEnabled(String provider)
- {
- }
- @Override
- public void onStatusChanged(String provider, int status, Bundle extras)
- {
- }
- }
- private class FindLocation extends AsyncTask<Void, Void, Location>
- {
- @Override
- protected Location doInBackground(final Void... params)
- {
- return endFind();
- }
- @Override
- protected void onPostExecute(final Location location)
- {
- if (locationRunnable != null)
- locationRunnable.locationUpdate(location);
- disableMyLocation();
- }
- }
- }
Add Comment
Please, Sign In to add comment