我正在尝试使用Smart Location Library获取定期位置更新。但是当启动进程时它会触发侦听器,然后就没有进一步的更新。这是我使用的代码
public void startTracking(View v) {
provider = new LocationGooglePlayServicesWithFallbackProvider(this);
Log.i("Tag","Start Tracking");
SmartLocation smartLocation = new SmartLocation.Builder(this)
.logging(true)
.build();
smartLocation.location(provider)
.config(LocationParams.BEST_EFFORT)
.start(this);
smartLocation.activity().start(this);
}
public void stopTracking(View v) {
Log.i("Tag","Stop Tracking");
SmartLocation.with(this).location().stop();
SmartLocation.with(this).activity().stop();
}
@Override
public void onActivityUpdated(DetectedActivity detectedActivity)
{
Log.i("Tag","ActivityUpdate : "+detectedActivity.toString());
}
@Override
public void onLocationUpdated(Location location)
{
Log.i("Tag","LocationUpdate : "+location.getLatitude()+","+location.getLongitude());
}
这是我得到的Logcat输出
01-27 14:09:01.791 3038-3038/com.angulusits.smartlocationtest I/Tag:
Start Tracking
01-27 14:09:01.879 3038-3038/com.angulusits.smartlocationtest I/Tag:
ActivityUpdate : DetectedActivity [type=STILL, confidence=100]
01-27 14:09:02.017 3038-3038/com.angulusits.smartlocationtest I/Tag:
LocationUpdate : <some-value>,<some-value>
一旦按下startButton,就会给出这个,然后再没有值。
我检查了Library for BEST_EFFORT值的代码:
public static final LocationParams BEST_EFFORT = new Builder().setAccuracy(LocationAccuracy.MEDIUM).setDistance(150).setInterval(2500).build();
所以我希望每个 2500毫秒的更新,即使设备没有移动。 如果有人能指出我正确的方向,我将不胜感激。
答案 0 :(得分:13)
创建位置监听器时需要说continuous()
。
请尝试下面的代码段,
private void startLocationListener() {
long mLocTrackingInterval = 1000 * 5; // 5 sec
float trackingDistance = 0;
LocationAccuracy trackingAccuracy = LocationAccuracy.HIGH;
LocationParams.Builder builder = new LocationParams.Builder()
.setAccuracy(trackingAccuracy)
.setDistance(trackingDistance)
.setInterval(mLocTrackingInterval);
SmartLocation.with(this)
.location()
.continuous()
.config(builder.build())
.start(new OnLocationUpdatedListener() {
@Override
public void onLocationUpdated(Location location) {
processLocation(location);
}
});
}
迟到的回复!无论如何,这会对某人有所帮助。
答案 1 :(得分:-3)
使用GoogleFuse Api要好得多。Here是这个Api的完整文档。它比使用Github库进行位置跟踪要好得多。