输出从科尔多瓦插件延迟 [英] Output delayed from Cordova Plugin

查看:143
本文介绍了输出从科尔多瓦插件延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个插件科尔多瓦下载文件并将其保存在数据文件夹。一切正常,除了返回值。我想显示一个进度条,并需要获得当前的进度。下面是相关的部分从我的code:

I've written a Cordova plugin to download a file and save it in the data folder. Everything is working fine except for the return value. I would like to display a progress bar and need to get the current progress. Here's the relevant part from my code:

 while ((readed = is.read(buffer)) > 0) {
     fos.write(buffer, 0, readed);
     totalReaded += readed;

     int newProgress = (int) (totalReaded*100/fileSize);
     if (newProgress != progress) {
         progress = newProgress;
         PluginResult res = new PluginResult(PluginResult.Status.OK, progress);
         res.setKeepCallback(true);
         callbackContext.sendPluginResult(res);
     }
  }

我的javascript:

My JavaScript:

downloader.prototype.writeFile = function (downloaderSuccess, downloaderFailure, options) {
    cordova.exec(downloaderSuccess, downloaderFailure, "downloader", "writeFile", options);
};

function downloaderSuccess(progress) {
    WL.Logger.debug("Result: "+progress)
}

function downloaderFailure(error) {
    WL.Logger.error("Error: "+error);
}

什么情况是在的的进展只会输出的文件已经被下载。如果我设置为PluginResult.Status NO_RESULT,也不会输出任何东西。

What happens is that the progress will only be output after the file has been downloaded. If I set the PluginResult.Status to NO_RESULT, it won't output anything at all.

推荐答案

您使用

   cordova.getThreadPool().execute(new Runnable() {
            public void run() {
                // while loop goes here
            }
        });

在下载code的开始?

in the start of your download code?

看一看<一个href=\"https://github.com/apache/cordova-android/blob/master/framework/src/org/apache/cordova/FileTransfer.java#L310\"相对=nofollow> SRC /组织/阿帕奇/科尔多瓦/ FileTransfer.java 。该文件确实pretty你在做什么很多,你可以看到他们如何能够发送实时进度更新,因为他们是在一个单独的线程。

Have a look at src/org/apache/cordova/FileTransfer.java. This files does pretty much what you are doing and you can see how they are able to send live progress updates because they are in a separate thread.

我认为问题是,无论是JavaScript的和Java code <一个href=\"http://docs.phonegap.com/en/2.5.0/guide_plugin-development_android_index.md.html#Developing%20a%20Plugin%20on%20Android\"相对=nofollow>在同一WebCore的线程中运行所以发生的是,下载阻止Java和用户界面之间的通信(例如,发送结果),直至下载完成。

I think the problem is that both the JavaScript and the Java code run in the same WebCore thread so what is happening is that the download is blocking the communication between Java and the UI (eg, sending the result) until the download is done.

如果你遵循不堵的WebCore线程指南中的建议(所以,使用: cordova.getThreadPool()执行()),你会能够得到进度更新。

If you follow the advice in that guide about not blocking the WebCore thread (so, use: cordova.getThreadPool().execute()), you'll be able to get progress updates.

要测试这一点,我把这个插件:的https://github.com/phonegap/phonegap-plugins/blob/master/Android/Downloader/Downloader.java
,做文件下载。因为它的立场,这code的行为和你的一样呢 - 这个文件被下载后,才更新进度发送。但是,一旦我裹在可运行将downloadURL()方法,它工作得很好,送进度更新,因为它们发生的,而不是等待,直到结束。

To test this, I took this plugin: https://github.com/phonegap/phonegap-plugins/blob/master/Android/Downloader/Downloader.java that does File downloading. As it stands, this code behaves the same as yours does - the progress update is sent only after the file is downloaded. However, once I wrapped the downloadUrl() method in the runnable, it worked fine, sending progress updates as they happened instead of waiting until the end.

请让和我知道这帮助,如果你想我想出了一个更好的解释。

Please let me know if this helps or if you'd like me to come up with a better explanation.

这篇关于输出从科尔多瓦插件延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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