簡體   English   中英

為什么我無法連接到GoogleApiClient?

[英]Why I can't connect to GoogleApiClient?

我正在嘗試連接到GoogleApiClient,但是什么也沒發生,並且GoogleApiClient.ConnectionCallbacks不會調用。connect()之后,我立即轉到onConnectionFailed。 如何解決?

主要活動:

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>

檢查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/

我覺得你需要

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

在你的清單上

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM