另一个" OnLocationChanged回调永远不会被调用"

时间:2017-05-19 13:42:33

标签: android geolocation google-api-client locationmanager

由于这是一个典型的问题,所以要求这么多次 - 是的,我已经检查过它们了。

这就是我的初始化方式:

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

这是我连接/断开的方式:

@Override
protected void onStart() {
    if (!mGoogleApiClient.isConnected()) {
        mGoogleApiClient.connect();
    }
    super.onStart();
}

@Override
protected void onStop() {
    mGoogleApiClient.disconnect();
    super.onStop();
}

调试它,我看到这叫做:

@Override
public void onConnected(@Nullable Bundle bundle) {

    Timber.i("onConnected");

}

后来,这也被称为:

    mLocationRequest = new LocationRequest();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(1000);

    mLastKnownLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

但这些都没有被称为:

@Override
public void onConnectionSuspended(int i) {
    Timber.i("onConnectionSuspended");
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    Timber.w("onConnectionFailed");
}

@Override
public void onLocationChanged(Location location) {
    Timber.d("onLocationChanged " + location);
    mLastKnownLocation = location;
    moveCamToCurrentLocation();
}

在我朋友的设备上它可以正常工作,而且它也常常在我的设备上工作。 我检查了SelfPermission()等,还检查了手机的高级设置,地图有效....我是否遇到了一个邪恶的无法管理的错误?我没有想法。顺便说一下,这是我的build.gradle的摘录:

compile 'com.facebook.stetho:stetho:1.5.0'
compile 'com.facebook.stetho:stetho-okhttp:1.5.0'
compile 'com.google.android.gms:play-services:10.2.0'
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:support-v4:25.3.1'
compile 'com.android.support:preference-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.1'
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.rockerhieu.emojicon:library:1.3.3'
compile 'com.jakewharton:butterknife:8.5.1'
compile 'com.jakewharton.timber:timber:4.5.0'
compile 'com.firebaseui:firebase-ui-auth:1.2.0'
compile 'com.google.firebase:firebase-database:10.2.0'
testCompile 'junit:junit:4.12'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'

设备会显示Google地图,我也会在此行获得有效位置:

    mLastKnownLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

我错过了什么吗?有没有人能为我提供线索?

0 个答案:

没有答案