Я пытаюсь подключиться к GoogleApiClient, но ничего не происходит, и GoogleApiClient.ConnectionCallbacks не вызывают. После подключения() я немедленно перехожу к onConnectionFailed. Как это исправить?Почему я не могу подключиться к GoogleApiClient?
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'
}
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>
Что ошибка вы получаете? – Soham
У меня нет ошибки, я вызываю соединение в экземпляре GoogleApiClient, а затем я перехожу к onConnectionFailed из GoogleApiClient.OnConnectionFailedListener. Я не понимаю, почему onConnected не вызывает –
Вы пробовали под кодом? – Soham