【问题标题】:onLocationChanged never called in FusedLocationApionLocationChanged 从未在 FusedLocationApi 中调用
【发布时间】:2017-06-05 09:33:07
【问题描述】:

关于同一问题还有其他一些关于 SO 的问题,但我想答案有点陈旧,可能不适用于我的场景。在下面的代码中,永远不会调用方法 onLocationChanged。我还尝试从 android 模拟器发送新位置,但它从未被调用过。知道这里出了什么问题吗?

public class MyActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, 
  GoogleApiClient.OnConnectionFailedListener, LocationListener {

    private GoogleApiClient apiClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myview);

        apiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();

        apiClient.connect();
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        System.out.println("yeah, this works");

        // I also tried to set some properties on the location request like interval, etc. but they have no effect
        LocationRequest request = new LocationRequest();
        LocationServices.FusedLocationApi.requestLocationUpdates(apiClient, request, this);
    }

    @Override
    public void onConnectionSuspended(int i) {
        System.out.println("this is never shown");
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        System.out.println("this is never shown");
    }

    @Override
    public void onLocationChanged(Location location) {
        System.out.println("this line is never called ");
    }
}

【问题讨论】:

  • 你有权限吗?
  • 这个例子过一遍,你可以从这里解决:javapapers.com/android/android-location-fused-provider
  • 嘿@shaft 我认为您必须首先检查 googlePlay 是否可用,然后再检查位置提供程序,然后再进一步检查代码
  • 谢谢你的链接,我现在试试。

标签: android location fusedlocationproviderapi


【解决方案1】:

我发现我有关于“所有 com.android.support 库必须使用完全相同的版本规范”的 gradle 警告。就我而言,我改变了

compile 'com.android.support:appcompat-v7:25.2.0'
compile 'com.android.support:support-v4:25.2.0'
compile 'com.android.support:design:25.2.0'
compile 'com.android.support:recyclerview-v7:25.2.0'
compile 'com.google.android.gms:play-services:9.8.0'
compile 'com.android.support.constraint:constraint-layout:1.0.2'

compile 'com.android.support:appcompat-v7:25.2.0'
compile 'com.android.support:animated-vector-drawable:25.2.0'
compile 'com.android.support:support-core-ui:25.2.0'
compile 'com.android.support:mediarouter-v7:25.2.0'
compile 'com.android.support:support-v4:25.2.0'
compile 'com.android.support:design:25.2.0'
compile 'com.android.support:recyclerview-v7:25.2.0'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.google.android.gms:play-services:9.8.0'

有了新的依赖列表,gradle 很高兴,这个例子也能正常工作。希望这可以帮助某人。

【讨论】:

    猜你喜欢
    • 2017-08-04
    • 2015-09-28
    • 2014-11-23
    • 2017-08-27
    • 2012-08-09
    • 1970-01-01
    • 1970-01-01
    • 2016-12-08
    • 2017-08-09
    相关资源
    最近更新 更多