为什么我无法连接到GoogleApiClient?

时间:2015-03-27 19:29:16

标签: java android

我正在尝试连接到GoogleApiClient,但没有任何事情发生,GoogleApiClient.ConnectionCallbacks也没有调用。在connect()之后,我立即转到onConnectionFailed。如何解决?

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 {
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'

}

清单:

<?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>

2 个答案:

答案 0 :(得分:0)

检查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;
    }
}

检查以下链接是否有帮助

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

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

答案 1 :(得分:0)

我认为你需要

<uses-permission android:name="android.permission.INTERNET" />
你的清单中的