简体   繁体   中英

how to wait for a certain callback method to be called before a function returns in java?

I am writing a phonegap plugin.My code outline is this

public PluginResult execute(String action, JSONArray arg1, String arg2) {
      try{
        if (action.equals("authenticate")) {
           this.startlogin();
           return new PluginResult(PluginResult.Status.OK);
        }
        else {
            return new PluginResult(PluginResult.Status.INVALID_ACTION);
        }
       }catch(Exception e){}
            return new PluginResult(PluginResult.Status.JSON_EXCEPTION); 

    }

startlogin() method calls a function which has callback method registered which gets called after certain event.ie startlogin() method returns but a certain method is called after an event occurs which is asynchronous.

 public void startlogin(){

    login();//this has callback method
 }

how to wait until that callback method is finished before i return from execute method? Whats the best method in java?

PS:I cannot change execute method prototype since phonegap plugin works on execute method.

Easiest approach:

if (this.startlogin()) ...

...

public boolean startlogin(){

    return login();
}

It seems to me that you're trying to mix synchronous with asynchronous...

What you should really do is have the callback method return the PluginResult.

Cheers,

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