Created
January 13, 2011 12:27
-
-
Save amay077/777790 to your computer and use it in GitHub Desktop.
[Android]TimeoutableLocationListner: TimeoutableLocationListner is implementation of LocationListener for Android. If onLocationChanged isn't called within XX mili seconds, automatically remove.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.amay077.android.location; | |
import java.util.Timer; | |
import java.util.TimerTask; | |
import android.location.Location; | |
import android.location.LocationListener; | |
import android.location.LocationManager; | |
import android.os.Bundle; | |
/** | |
* TimeoutableLocationListner is implementation of LocationListener. | |
* If onLocationChanged isn't called within XX mili seconds, | |
* automatically remove listener. | |
* | |
* @author amay077 - http://twitter.com/amay077 2011.1.12 | |
*/ | |
public abstract class TimeoutableLocationListener | |
implements LocationListener { | |
protected Timer timerTimeout = new Timer(); | |
protected LocationManager locaMan = null; | |
/** | |
* Initialize instance. | |
* | |
* @param locaMan the base of LocationManager, can't set null. | |
* @param timeOutMS timeout elapsed (mili seconds) | |
* @param timeoutListener if timeout, call onTimeouted method of this. | |
*/ | |
public TimeoutableLocationListener(LocationManager locaMan, long timeOutMS, | |
final TimeoutLisener timeoutListener) { | |
this.locaMan = locaMan; | |
timerTimeout.schedule(new TimerTask() { | |
@Override | |
public void run() { | |
if (timeoutListener != null) { | |
timeoutListener.onTimeouted( | |
TimeoutableLocationListener.this); | |
} | |
stopLocationUpdateAndTimer(); | |
} | |
}, timeOutMS); | |
} | |
/*** | |
* Location callback. | |
* | |
* If override on your concrete class, must call base.onLocation(). | |
*/ | |
@Override | |
public void onLocationChanged(Location location) { | |
stopLocationUpdateAndTimer(); | |
} | |
@Override | |
public void onProviderDisabled(String s) { } | |
@Override | |
public void onProviderEnabled(String s) { } | |
@Override | |
public void onStatusChanged(String s, int i, Bundle bundle) { } | |
private void stopLocationUpdateAndTimer() { | |
locaMan.removeUpdates(this); | |
timerTimeout.cancel(); | |
timerTimeout.purge(); | |
timerTimeout = null; | |
} | |
public interface TimeoutLisener { | |
void onTimeouted(LocationListener sender); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment