Java如何检测变量

时间:2015-05-12 00:11:58

标签: java javascript android cordova plugins

我有cordova插件,它正在工作。但是,等待hashmap的while循环似乎效率低下。还有更好的方法吗?

public class MyApp extends CordovaPlugin {
  static HashMap<String,String> myProp = new HashMap<String,String>();

  protected void pluginInitialize() {
  }

  public boolean execute(String action, JSONArray args, CallbackContext callbackContext) 
      throws JSONException {
    if (action.equals("init")) {
      AppConnectionService.requestConfig(cordova.getActivity());

      init(callbackContext);

      PluginResult.Status status = PluginResult.Status.NO_RESULT;
      PluginResult pluginResult = new PluginResult(status);
      pluginResult.setKeepCallback(true);
      callbackContext.sendPluginResult(pluginResult);    

      return true;      
    }
    return false;
  }

  private void init(final CallbackContext callbackContext) {
    while(myProp.isEmpty()) { 
    }

    PluginResult result = new PluginResult(PluginResult.Status.OK, myProp.get("myID"));
    result.setKeepCallback(false);
    callbackContext.sendPluginResult(result);         
  }

  public static boolean handleConfig (Bundle config) {
      if (config != null) {
        // Add all the entries that came in the config for the display adapter
        for (String key: config.keySet()) {            
          myProp.put(key, config.getString(key));
        }
      } else {
          Log.d("MyApp", "Failed");
      }
      return false;
  }  
}

因此,在JavaScript部分,它将调用init(),并返回“myID”,但我等待结果的方式是一个繁忙的循环:

while(myProp.isEmpty()) { 
}

是否有一种更有效,更健壮的方式来等待结果?