GoogleApiClient.ConnectionCallbacks methods not being called after connecting to the GoogleApiClient

坚强是说给别人听的谎言 提交于 2019-12-11 20:24:26

问题


I've got some code which is connecting to the GoogleApiClient but onConnected is not being called.

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) {
    }

None of the four @Override methods are being called, why is that?


回答1:


You need to call addConnectionCallbacks() and addOnConnectionFailedListener() on your GoogleApiClient.Builder:

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


来源:https://stackoverflow.com/questions/28053332/googleapiclient-connectioncallbacks-methods-not-being-called-after-connecting-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!