Created
December 21, 2015 07:37
-
-
Save rajkshah14/ade4b88333294fdb177c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import android.location.Criteria; | |
import android.location.Location; | |
import android.location.LocationListener; | |
import android.location.LocationManager; | |
import android.os.Bundle; | |
import android.os.Looper; | |
import rx.Observable; | |
import rx.Subscriber; | |
public class LocationService { | |
private final LocationManager mLocationManager; | |
public LocationService(LocationManager locationManager) { | |
mLocationManager = locationManager; | |
} | |
public Observable<Location> getLocation() { | |
return Observable.create(new Observable.OnSubscribe<Location>() { | |
@Override | |
public void call(final Subscriber<? super Location> subscriber) { | |
final LocationListener locationListener = new LocationListener() { | |
public void onLocationChanged(final Location location) { | |
subscriber.onNext(location); | |
subscriber.onCompleted(); | |
Looper.myLooper().quit(); | |
} | |
public void onStatusChanged(String provider, int status, Bundle extras) { | |
} | |
public void onProviderEnabled(String provider) { | |
} | |
public void onProviderDisabled(String provider) { | |
} | |
}; | |
final Criteria locationCriteria = new Criteria(); | |
locationCriteria.setAccuracy(Criteria.ACCURACY_COARSE); | |
locationCriteria.setPowerRequirement(Criteria.POWER_LOW); | |
final String locationProvider = mLocationManager | |
.getBestProvider(locationCriteria, true); | |
Looper.prepare(); | |
mLocationManager.requestSingleUpdate(locationProvider, | |
locationListener, Looper.myLooper()); | |
Looper.loop(); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment