简体   繁体   English

如何在调用任何方法之前确保它已连接()? 我的方法被称为GooglePlus身份验证之后

[英]How do I make sure that it is connected() before any method is invoked? My methods are beight called AFTER the GooglePlus authentication

Please see the following LogCat output. 请查看以下LogCat输出。 In Base.OnStart() I am calling mPlusClient.connect(); 在Base.OnStart()中,我正在调用mPlusClient.connect();。 Now I am calling various methods in ShowLog.OnStart, but just because the Google is not yet authenticated, it throws error "GoogleApiClient is not connected". 现在,我正在ShowLog.OnStart中调用各种方法,但是仅仅因为Google尚未通过身份验证,它会引发错误“ GoogleApiClient未连接”。

What should I do to change my design, so that it always connects and then only I call the various methods. 我应该怎么做才能更改设计,以使其始终保持连接,然后只有我调用各种方法。

mainActivity.onCreateOptionsMenu called
Base.onConnectionFailed called
onClick called
onActivityResult called
Base: onConnected called. Is Google connected?true
showLog: onCreate
Base: onCreate
showLog: onCreate: is Google connected?false
showLog: onStart
Base: onStart
Base: onStart. Is Google connected?false
**showLog: onStart: is Google connected?false**
showLogsActivity.onCreateOptionsMenu called
Base: onConnected called. Is Google connected? **true**
Base: onStop
showLog: onStop
Base: onStop

I am also including the code below (I am removing some code to keep it concise). 我还将包括以下代码(为了使内容简洁,我正在删除一些代码)。 Here is the complete code: http://codeviewer.org/view/code:52b6 这是完整的代码: http : //codeviewer.org/view/code : 52b6

Thanks EDIT 1* After suggestion I have the code modified as: 感谢编辑1 *建议后,我将代码修改为:

Class showLog{

@Override
    void onGooglePlayServicesConnected() {
        if (mPlusClient.isConnected()) {
            //background process to get Picasa Photo
            AsynTaskGetPicasaMedia objAsyncTaskGetPicasaMedia = new AsynTaskGetPicasaMedia(getApplicationContext());
            objAsyncTaskGetPicasaMedia.execute();
        }

    }

}

Now in class AsynTaskGetPicasaMedia 现在在类AsynTaskGetPicasaMedia中

 @Override
    protected Boolean doInBackground(Void... params) {


        final String accountName = Plus.AccountApi.getAccountName(BaseActivity.mPlusClient);
}

I am still getting the error! 我仍然遇到错误!

If your base class implements the callbacks to google play services then you create a method in that base class that you call when its connected. 如果您的基类实现了对Google Play服务的回调,那么您将在该基类中创建一个在连接时调用的方法。

example

@Override
public void onConnected(Bundle connectionHint) {
    onGooglePlayServicesConnected();
}

public void onGooglePlayServicesConnected(){

}

in your activity that extends this base class just override the method onGooglePlayServicesConnected() 在扩展此基类的活动中,只需重写onGooglePlayServicesConnected()方法

@Override
public void onGooglePlayServicesConnected() {
    //do stuff here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM