谷歌Api客户端无法连接



尝试获取当前位置的纬度和经度。我想这是因为谷歌ApiClient无法连接。因此,我的onConnected方法没有被调用,这就是我获得纬度和经度值的地方。我试过了这里的所有建议,但仍然不起作用。

注意:当我运行应用程序时,我也不会出现任何错误。

主要活动.java

package com.example.android.location2;
import android.content.pm.PackageManager;
import android.location.Location;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnSuccessListener;
import static android.Manifest.permission.ACCESS_FINE_LOCATION;
public class MainActivity extends AppCompatActivity implements ConnectionCallbacks, OnConnectionFailedListener {
public static final int RequestPermissionCode = 1;
private GoogleApiClient googleApiClient;
private TextView longitudeText;
private TextView latitudeText;
protected Location lastLocation;
private FusedLocationProviderClient fusedLocationProviderClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
longitudeText = findViewById(R.id.longitude_text);
latitudeText =  findViewById(R.id.latitude_text);

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

fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(MainActivity.this);
}
@Override
protected void onStart() {
//googleApiClient.connect();
super.onStart();
this.googleApiClient.connect();
// Here i check if the googleApiClient is connected by printing  
if (this.googleApiClient.isConnected()){
Toast.makeText(this, "connected", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "Not connected", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onStop() {
if(googleApiClient.isConnected()) {
googleApiClient.disconnect();
}
super.onStop();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
if (ActivityCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermission();
} else {
fusedLocationProviderClient.getLastLocation()
.addOnSuccessListener(MainActivity.this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
// Got last known location. In some rare situations this can be null.
if (location != null) {
// Logic to handle location object
latitudeText.setText(String.valueOf(location.getLatitude()));
longitudeText.setText(String.valueOf(location.getLongitude()));
}
}
});
}
}

private void requestPermission() {
ActivityCompat.requestPermissions(MainActivity.this, new
String[]{ACCESS_FINE_LOCATION}, RequestPermissionCode);
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.e("MainActivity", "Connection failed: " + connectionResult.getErrorCode());
}
@Override
public void onConnectionSuspended(int i) {
Log.e("MainActivity", "Connection suspendedd");
}
}

您确定GoogleApiClient连接有问题吗?你的Toast出现了吗?

有一个关于使用Google Fused API获取最后一个已知位置的教程。如果这是一个清单问题,本教程还包含了一个清单。它与您想要的并不完全相同,但我认为它可能有助于找出实现出错的地方。

相关内容

  • 没有找到相关文章

最新更新