Back to project page rex-weather.
The source code is released under:
Copyright (c) 2014, Vy-Shane Sin Fat All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met...
If you think the Android project rex-weather listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package mu.node.rexweather.app.Services; //from ww w . j a v a2 s.co m 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; /** * Implement an Rx-style location service by wrapping the Android LocationManager and providing * the location result as an Observable. */ 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(); } }); } }