I can't seem to make GoogleApiClient
to work on my project. I'm using LocationServices
, OnConnected
is not firing even though i have the client.connect()
called in the onStart
method.
I've searched stackoverflow and tried solutions given but i still cant make it work.
i tried checking if i was connected but client.isConnected()
outputs false always.
my permissions are:
android.permission.INTERNET
android.permission.ACCESS_COARSE_LOCATION
android.permission.ACCESS_FINE_LOCATION
am i missing something here?
edit:
private GoogleApiClient client;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
ButterKnife.inject(this);
buildGoogleApiClient();
}
protected synchronized void buildGoogleApiClient() {
client = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API).build();
}
@Override
public void onStart() {
// client.connect();
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
Log.e("Connected?", String.valueOf(client.isConnected()));
}
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.e("Failed?", connectionResult.getErrorMessage());
}
@Override
public void onConnected(Bundle bundle) {
if ( ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {
return;
}
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
client);
longt = mLastLocation.getLongitude();
latt = mLastLocation.getLatitude();
if (mLastLocation != null) {
// mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude()));
// mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude()));
}
}