【问题标题】:Calling a javascript function from java method in Cordova从 Cordova 中的 java 方法调用 javascript 函数
【发布时间】:2014-09-20 05:21:14
【问题描述】:

我为javascript和本地java代码之间的通信制作了一个插件。但由于某种原因,我不得不立即返回回调。反过来,我在java中启动了一个异步方法。现在完成实现后,我想回到我的javascript。请告诉我该怎么做。

我的java代码是-

public class MyPlugin extends CordovaPlugin
{
    String fileName;
    Context context;
    @Override
    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
        super.initialize(cordova, webView);
        // your init code here
    }

     @Override
     public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException 
     {
            context=this.cordova.getActivity().getApplicationContext();
            String myurl = args.getString(0);
            if (action.equals("plugin1"))
            { 

                   new DownloadManager().execute(myurl);

                    callbackContext.success("Operation performed successfully!!");
                    return true;
             }


            callbackContext.error("error");
            return false;

     }

     public class DownloadManager extends AsyncTask<String, String, String> {

        @Override
        public String doInBackground(String... arg0) {
            downloadapk(arg0[0]);
            installapk();
            System.out.println("Download Complete");
            return null;
//Here I want to return to javascript.How can I callback to the javascript function from //async method?? 
        }

     }

【问题讨论】:

    标签: java javascript cordova


    【解决方案1】:

    您必须将callbackContext 传递给DownloadManager 类。

     @Override
     public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException 
     {
            context=this.cordova.getActivity().getApplicationContext();
            String myurl = args.getString(0);
            if (action.equals("plugin1"))
            {
                // Pass callbackContext to your actual code which performing the work.
                new DownloadManager(callbackContext).execute(myurl);
                return true;
            }
    
            callbackContext.error("error");
            return false;
     }
    
     public class DownloadManager extends AsyncTask<String, String, String> {
        private CallbackContext callbackContext;
        public DownloadManager(CallbackContext callbackContext) {
             this.callbackContext = callbackContext;
        }
    
        @Override
        public String doInBackground(String... arg0) {
            downloadapk(arg0[0]);
            installapk();
            this.callbackContext.success();
            return null;
            //Here I want to return to javascript.How can I callback to the javascript function from //async method?? 
        }
    

    【讨论】:

    • 感谢您的回答。这对我帮助很大。
    猜你喜欢
    • 1970-01-01
    • 2017-07-11
    • 2018-07-30
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多