问题
I have gone through all the StackOverflow questions and tried every method but still the problem persists.
public class PlaceInfoActivity extends ActionBarActivity implements
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private static final String LOG_TAG = "PlaceInfoActivity";
private static final int GOOGLE_API_CLIENT_ID = 0;
private GoogleApiClient mGoogleApiClient;
TextView mTextView;
String content = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_place_info);
mTextView = (TextView) findViewById(R.id.textView0);
mGoogleApiClient = new GoogleApiClient
.Builder(this)
.enableAutoManage(this, 0, this)
.addApi(Places.PLACE_DETECTION_API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
Toast.makeText(this, Boolean.toString(mGoogleApiClient.isConnected()), Toast.LENGTH_SHORT).show();
if (mGoogleApiClient.isConnected()) {
PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi.getCurrentPlace(mGoogleApiClient, null);
result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
@Override
public void onResult(PlaceLikelihoodBuffer likelyPlaces) {
PlaceLikelihood placeLikelihood = likelyPlaces.get(0);
if (placeLikelihood != null && placeLikelihood.getPlace() != null && !TextUtils.isEmpty(placeLikelihood.getPlace().getName()))
content = "Most likely place: " + placeLikelihood.getPlace().getName() + "\n";
if (placeLikelihood != null)
content += "Percent change of being there: " + (int) (placeLikelihood.getLikelihood() * 100) + "%";
mTextView.setText(content);
likelyPlaces.release();
}
});
}
}
@Override
protected void onStart() {
super.onStart();
if (mGoogleApiClient != null)
mGoogleApiClient.connect();
}
@Override
protected void onResume() {
super.onResume();
mGoogleApiClient.connect();
}
@Override
protected void onStop() {
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
super.onStop();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
@Override
public void onConnected(Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
} }
After building when I print the toast it gives false everytime, what could be the error? I am following this tutorial and want to retrieve the name of my current place. http://code.tutsplus.com/articles/google-play-services-using-the-places-api--cms-23715
回答1:
The Problem is with mGoogleApiClient.isConnected(), it gives you false and that's why it is unable to perform the further working. If you just remove the if(mGoogleApiClient.isConnected()) part, it will work.
Do it like this:
public void showPlaces() {
PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi.getCurrentPlace(mGoogleApiClient, null);
result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
@Override
public void onResult(PlaceLikelihoodBuffer likelyPlaces) {
StringBuffer sb=new StringBuffer();
for (PlaceLikelihood placeLikelihood : likelyPlaces) {
String name, per;
name = placeLikelihood.getPlace().getName().toString()+" ";
per = ""+(int) (placeLikelihood.getLikelihood() * 100)+"\n";
sb.append(name+per);
}
content = sb.toString();
mTextView.setText(content);
likelyPlaces.release();
}
});
}
And call the showPlaces method, just after mGoogleApiClient....build();
mGoogleApiClient = new GoogleApiClient
.Builder(this)
.enableAutoManage(this, 0, this)
.addApi(Places.PLACE_DETECTION_API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
showPlaces();
回答2:
it always false because you call connect()
onStart
. Declare a method
public void showPlaces() {
PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi.getCurrentPlace(mGoogleApiClient, null);
result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
@Override
public void onResult(PlaceLikelihoodBuffer likelyPlaces) {
PlaceLikelihood placeLikelihood = likelyPlaces.get(0);
if (placeLikelihood != null && placeLikelihood.getPlace() != null && !TextUtils.isEmpty(placeLikelihood.getPlace().getName()))
content = "Most likely place: " + placeLikelihood.getPlace().getName() + "\n";
if (placeLikelihood != null)
content += "Percent change of being there: " + (int) (placeLikelihood.getLikelihood() * 100) + "%";
mTextView.setText(content);
likelyPlaces.release();
}
});
}
and call it, when onConnected
is invoked
@Override
public void onConnected(Bundle bundle) {
showPlaces();
}
来源:https://stackoverflow.com/questions/30356615/mgoogleapiclient-isconnected-returns-null