我有一些代码连接到GoogleApiClient但是没有调用onConnected。
public class MainActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private GoogleApiClient mApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initGoogleApiClient();
}
private void initGoogleApiClient() {
mApiClient = new GoogleApiClient.Builder( this )
.addApi( Wearable.API )
.build();
mApiClient.connect(); // On completion onConnected() will be called
}
@Override
public void onConnected(Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
protected void onDestroy() {
super.onDestroy();
mApiClient.disconnect();
}
@Override
public void onConnectionFailed(com.google.android.gms.common.ConnectionResult connectionResult) {
}
没有调用四个@Override方法,为什么会这样?
答案 0 :(得分:5)
您需要在GoogleApiClient.Builder
上致电addConnectionCallbacks()和addOnConnectionFailedListener():
mApiClient = new GoogleApiClient.Builder( this )
.addApi( Wearable.API )
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();