尝试使用ReactiveLocation(https://github.com/mcharmas/Android-ReactiveLocation)根据用户活动更新Google Play服务的位置间隔。 GPS首次打开,但我不认为间隔会根据活动而改变。
MyLocation类
public MyLocation(Context context) {
this.context = context;
startLocationSensor();
}
public static MyLocation getInstance(Context context) {
if ( instance == null){
Log.i(TAG,"creating new Location sensor");
instance = new MyLocation(context);
}
return instance;
}
public void startLocationSensor(){
locationProvider = new ReactiveLocationProvider(context);
locationRequest = LocationRequest.create()
.setFastestInterval(1000 * 10) //Do not receive the updated any frequent than 10 sec
.setInterval(1000 * 20) // Receive location update every 20 sec
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
lastKnownLocationObservable = locationProvider
.getLastKnownLocation()
.subscribeOn(Schedulers.newThread())
.observeOn(Schedulers.newThread());
activityObservable = locationProvider
.getDetectedActivity(1000 * 20); // Get updated every 20 sec
/ * setter and getter for the location interval * /
public void setLocationRequestInterval(long locationRequestInterval) {
this.locationRequest.setInterval(locationRequestInterval);
}
public long getLocationRequestInterval() {
return this.locationRequest.getInterval();
}
}
在我的服务中
public void onCreate() {
myLocation = MyLocation.getInstance(getApplicationContext());
startObserving();
}
public void startObserving() {
Observable<JSONObject> o1 = /* Accelerometer Sensor obeservable */
Observable<JSONObject> o2 = myLocation.getActivityObservable()
.observeOn(Schedulers.newThread())
.map(new ToMostProbableActivity())
.map(new DetectedActivityToJson())
.doOnError(new ErrorHandler());
Observable<JSONObject> o3 = myLocation.getLocationUpdatesObservable()
.observeOn(Schedulers.newThread())
.map(new LocationToJsonFunc())
.doOnError(new ErrorHandler());
r = Observable.combineLatest(o1, o2, o3, new Func4<JSONObject, JSONObject, JSONObject, JSONObject>() {
@Override
public JSONObject call(JSONObject s1, JSONObject s2, JSONObject s3) {
return someCombinedJsonObject
}
}).filter(new Func1<JSONObject, Boolean>() {
@Override
public Boolean call(JSONObject jsonObject) {
acty = "get current activity from jsonObject"
float LocationRequestInterval = myLocation.getLocationRequestInterval();
if (acty.equalsIgnoreCase("still")) {
sendMessage = false; // Not interested in these message
if (LocationRequestInterval == 1000*20){
Log.d(TAG,"Dont need frequent updates, set to longer interval");
myLocation.setLocationRequestInterval(1000 * 60 * 15); // 15 minutes
}
} else {
if (LocationRequestInterval == 1000*60*15){
Log.d(TAG,"user is moving so need frequent updates");
myLocation.setLocationRequestInterval(1000 * 20); // get location update every 20 seconds
}
}
return sendMessage;
}
}
);
//subscribe to the combined observable and perform update
combinedDataSubscription = r
.sample(5, TimeUnit.SECONDS)
.subscribe(new Action1<JSONObject>() {
@Override
public void call(JSONObject resultJson) {
doSomethingwithData(resultJson);
}
}
);
}