AsyncTask inside execute method of Cordova plugin not working properly

☆樱花仙子☆ 提交于 2019-12-10 22:38:20

问题


I am developing cordova plugin for the first time and stuck in the following issue.

I have created a class extending CorodvaPlugin and override execute method as given . What I want is after the asynctask has completed it background task, response is returned to the JS and values are displayed on the HTML but whats happening sometimes values are displayed and sometimes not.Any help would be appreciated.

@Override
public boolean execute(String action, JSONArray args,
        CallbackContext callbackContext) throws JSONException {
    try {

        context = this.cordova.getActivity().getApplicationContext();
            this.mMyCallbackContext = callbackContext;
            new WSCall().execute();
            PluginResult pluginResult = new  PluginResult(PluginResult.Status.NO_RESULT); 
            pluginResult.setKeepCallback(true); 
            mMyCallbackContext .sendPluginResult(pluginResult);
            return true;    


    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return false;
    }

}

and in the Async Task post execute I have done this

       @Override
        protected void onPostExecute(String result) {


            PluginResult result_;
            if(groups!=null)
                result_  = new PluginResult(PluginResult.Status.OK, groups); 
            else if(ret_msg!=null)
                result_  = new PluginResult(PluginResult.Status.OK, ret_msg); 
            else
                result_  = new PluginResult(PluginResult.Status.OK, ""); 

            result_.setKeepCallback(false); 
            mMyCallbackContext.sendPluginResult(result_);
            pDialog.dismiss();

        }

回答1:


Use this link and don't return true from execute method ,return Pluginresult only.



来源:https://stackoverflow.com/questions/25780372/asynctask-inside-execute-method-of-cordova-plugin-not-working-properly

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