android - 使用智能位置库的问题

标签 android location

我正在尝试使用 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 时立即给出的,然后没有更多的值。

我检查了库中的代码以获得 BEST_EFFORT 值:

public static final LocationParams BEST_EFFORT = new Builder().setAccuracy(LocationAccuracy.MEDIUM).setDistance(150).setInterval(2500).build();

所以我希望每 2500 毫秒更新一次,即使设备没有移动也是如此。 如果有人能指出正确的方向,我将不胜感激。

最佳答案

您需要在创建位置监听器时调用 continuous()。 试试下面的代码 fragment ,

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);
                }
            });
}

回复晚了!无论如何,这会对某人有所帮助。

关于android - 使用智能位置库的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35032880/

相关文章:

android - 如何将颜色资源 ID 的 int 数组从 array.xml 传递到 SwipeRefreshLayout.setColorSchemeResources

math - 使用两个经度/纬度点获取方向(罗盘)

Android:强制使用 Wi-Fi 作为位置提供者

ios - 第一次加载应用程序时,MKMapview ios 不显示当前位置

android - 如何在操作栏 Sherlock 中添加搜索

java - 循环 AsyncTask 类并获取 Json 并将其作为对象存储在列表中

android - Gamma 曲线实验——将 2.2 转换为 1.8 的相反值?

android - 获取 Android 应用程序启动的端口

ios - 未调用 didDetermineState、didEnterRegion、didExitRegion 事件

PHP 更改页面?