AsyncTask的执行里面科尔多瓦方法插件无法正常工作 [英] AsyncTask inside execute method of Cordova plugin not working properly

查看:177
本文介绍了AsyncTask的执行里面科尔多瓦方法插件无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发首次科尔多瓦插件并卡在以下问题。

我已经创建扩展给出CorodvaPlugin和覆盖执行方法的类。在AsyncTask的完成后,后台任务,响应返回给JS和值显示在HTML,但有时什么都发生的显示值有时not.Any帮助将是AP preciated我想是。

  @覆盖
公共布尔执行(串动,JSONArray ARGS,
        CallbackContext callbackContext)抛出JSONException {
    尝试{        上下文= this.cordova.getActivity()getApplicationContext()。
            this.mMyCallbackContext = callbackContext;
            新WSCall()执行();
            PluginResult pluginResult =新PluginResult(PluginResult.Status.NO_RESULT);
            pluginResult.setKeepCallback(真);
            mMyCallbackContext .sendPluginResult(pluginResult);
            返回true;
    }赶上(例外五){
        // TODO自动生成catch块
        e.printStackTrace();
        返回false;
    }}

和在异步任务执行后我已经这样做了。

  @覆盖
        保护无效onPostExecute(字符串结果){
            PluginResult result_;
            如果(组!= NULL)
                result_ =新PluginResult(PluginResult.Status.OK,团体);
            否则,如果(ret_msg!= NULL)
                result_ =新PluginResult(PluginResult.Status.OK,ret_msg);
            其他
                result_ =新PluginResult(PluginResult.Status.OK,);            result_.setKeepCallback(假);
            mMyCallbackContext.sendPluginResult(result_);
            pDialog.dismiss();        }


解决方案

<一个href=\"http://stackoverflow.com/questions/7751522/android-phonegap-notify-javascript-when-an-asynctask-is-finished/7849762#7849762\">Use此链接
而无法返回执行方法true,则返回Pluginresult只。

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();

        }

解决方案

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

这篇关于AsyncTask的执行里面科尔多瓦方法插件无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆