Created
January 12, 2017 13:52
-
-
Save naz013/dde9182a699552973ffba3b1048f5793 to your computer and use it in GitHub Desktop.
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
@SuppressWarnings("MissingPermission") | |
public class LocationTracker implements LocationListener { | |
private Context mContext; | |
private LocationManager mLocationManager; | |
private Callback mCallback; | |
public LocationTracker(Context context, Callback callback) { | |
this.mContext = context; | |
this.mCallback = callback; | |
updateListener(); | |
} | |
@Override | |
public void onLocationChanged(Location location) { | |
double latitude = location.getLatitude(); | |
double longitude = location.getLongitude(); | |
if (mCallback != null) mCallback.onChange(latitude, longitude); | |
} | |
@Override | |
public void onStatusChanged(String provider, int status, Bundle extras) { | |
updateListener(); | |
} | |
@Override | |
public void onProviderEnabled(String provider) { | |
updateListener(); | |
} | |
@Override | |
public void onProviderDisabled(String provider) { | |
updateListener(); | |
} | |
public void removeUpdates() { | |
mLocationManager.removeUpdates(this); | |
} | |
private void updateListener() { | |
if (mContext == null) return; | |
mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); | |
long time = (Prefs.getInstance(mContext).getTrackTime() * 1000) * 2; | |
int distance = Prefs.getInstance(mContext).getTrackDistance() * 2; | |
if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { | |
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, time, distance, this); | |
} else { | |
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, time, distance, this); | |
} | |
} | |
public interface Callback { | |
void onChange(double lat, double lon); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment