2
关于同样的问题,还有一些关于SO的其他问题,但我猜测答案有点旧,可能不适用于我的场景。在下面的代码中,方法onLocationChanged
从不被调用。我也尝试从android模拟器发送新的位置,但它从来没有被调用过。任何想法在这里出了什么问题?onLocationChanged从未在FusedLocationApi中调用过
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 ");
}
}
你有权限吗? – gaurang
通过这个例子一次,你可以从这里解决它:http://javapapers.com/android/android-location-fused-provider/ –
嘿@shaft我认为你必须首先检查谷歌播放是否可用和第二次检查位置提供商之后,进一步在代码 – santoXme