简体   繁体   中英

Why I can't connect to GoogleApiClient?

I am trying to connect to GoogleApiClient, but nothing is happend and GoogleApiClient.ConnectionCallbacks don't invoke.After connect() I immediately go to onConnectionFailed. How to fix it?

MainActivity:

public class MainActivity extends ActionBarActivity
    implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

private GoogleApiClient mGoogleApiClient;
private static final String TAG = MainActivity.class.getSimpleName();
private TextView tvLocation;
private boolean mResolvingError;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tvLocation = (TextView) findViewById(R.id.tv_location);
    buildGoogleApiClient();


    if(mGoogleApiClient.isConnecting()){
        Toast.makeText(this,"Connecting",Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(this,"Connecting stoped",Toast.LENGTH_LONG).show();
    }
}


public void buildGoogleApiClient(){
    mGoogleApiClient = new GoogleApiClient.Builder(this,this,this)
            .useDefaultAccount()
            .addApi(LocationServices.API)
            .build();
    mGoogleApiClient.connect();

}

@Override
protected void onStart() {
    super.onStart();
    if (!mResolvingError) {  
        mGoogleApiClient.connect();
    }

}

@Override
public void onConnected(Bundle bundle) {
    Log.d(TAG, "API Client Connected");
    LocationRequest request = LocationRequest.create();
    request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,request,new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            String loc = "lon=" + location.getLongitude() + "lat="+ location.getLatitude();
            tvLocation.setText(loc);
        }
    });

}

@Override
public void onConnectionSuspended(int i) {
    Log.d(TAG,"API Client Connected Failed");
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.d(TAG,"API Client Connection Problems");
}

}

Gradle dependencies:

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.google.android.gms:play-services:7.0.0'

}

Manifest:

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="ANDROID.PERMISSION.ACCESS_COARSE_LOCATION"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <meta-data android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Check if (!isGooglePlayServicesAvailable())

private boolean isGooglePlayServicesAvailable() {
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    if (ConnectionResult.SUCCESS == status) {
        return true;
    } else {
        GooglePlayServicesUtil.getErrorDialog(status, this, 0).show();
        return false;
    }
}

Check the below links if it can help

http://www.androidhive.info/2015/02/android-location-api-using-google-play-services/

http://javapapers.com/android/android-location-fused-provider/

I think you need

<uses-permission android:name="android.permission.INTERNET" />

in your manifest

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM