2
同じ問題については他にもいくつかの質問がありますが、答えは少し古く、私のシナリオにはあてはまらないかもしれません。以下のコードでは、メソッドonLocationChanged
は呼び出されません。私もアンドロイドエミュレータから新しい場所を送信しようとしましたが、それは決して呼び出されません。何がここで間違っているのだろうか?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私はあなたが最初にgooglePlayをチェックする必要があると思います。 2番目の場所のプロバイダのチェックの後にコードで行く – santoXme