简体   繁体   中英

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. In Base.OnStart() I am calling 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".

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

Thanks EDIT 1* After suggestion I have the code modified as:

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

 @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.

example

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

public void onGooglePlayServicesConnected(){

}

in your activity that extends this base class just override the method onGooglePlayServicesConnected()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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