Android 在FusedLocationApi中从未调用onLocationChanged

Android 在FusedLocationApi中从未调用onLocationChanged,android,location,fusedlocationproviderapi,Android,Location,Fusedlocationproviderapi,关于同一个问题还有其他一些问题,但我想答案有点陈旧,可能不适用于我的场景。在下面的代码中,永远不会调用方法onLocationChanged。我还尝试从android emulator发送新的位置,但从未调用过。知道这里出了什么问题吗 public class MyActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFai

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

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'


有了新的依赖项列表,gradle很高兴,而且这个例子很有效。希望这能帮助别人。

你有权限吗?再看一次这个例子,你可能会从下面的问题中解决:嘿@shaft我想你必须先检查googlePlay是否可用,然后再检查位置提供商。在代码中进一步感谢链接,我现在就试试。
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'