android - onLocationChanged 从未在 FusedLocationApi 中调用

标签 android location fusedlocationproviderapi

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

最佳答案

我发现我有关于“所有 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 很高兴并且示例正常运行。希望这可以帮助某人。

关于android - onLocationChanged 从未在 FusedLocationApi 中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44365861/

相关文章:

Android NumberFormatException 错误,从 EditText 输入一个简单的数字

android - 从主机的 eclipse 构建并运行第二台机器的 androidx86(在 virtualbox 中)

android - 手动设置纬度和经度后,谷歌地图不显示(缩放到)当前位置

android - Android 上的定期前台位置跟踪

Android: AnimationDrawable 转换错误

Android:屏幕旋转、销毁和服务难题

xcode - Swift - 当前位置的自己的结构/类(CLLocationManager)

iphone - 在 didSelectRowAtIndexPath 中获取用户位置坐标(用于 TableView Cell 点击)

android - 不幸的是,MyApp 已停止 - logcat 中没有错误日志