连接到GoogleApiClient后未调用GoogleApiClient.ConnectionCallbacks方法

时间:2015-01-20 19:10:13

标签: android google-api-client

我有一些代码连接到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方法,为什么会这样?

1 个答案:

答案 0 :(得分:5)

您需要在GoogleApiClient.Builder上致电addConnectionCallbacks()addOnConnectionFailedListener()

mApiClient = new GoogleApiClient.Builder( this )
    .addApi( Wearable.API )
    .addConnectionCallbacks(this)
    .addOnConnectionFailedListener(this)
    .build();