googleApiClient无法连接

时间:2018-11-04 14:27:22

标签: java android google-maps android-studio google-api-client

尝试获取我当前位置的纬度和经度。我认为是因为googleApiClient无法连接。结果,未调用我的onConnected方法,这是我获取经度和纬度值的地方。我已经在这里尝试了所有建议,但仍然无法正常工作。

注意:我在运行该应用程序时也没有任何错误。

MainActivity.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");
    }
}

1 个答案:

答案 0 :(得分:0)

您确定是GoogleApiClient连接问题吗?您的Toast中有没有出现?

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