2013-06-26 35 views
0

我有一个phonegap应用程序(Android)。我使用插件Downloader下载xml文件。 当我下载文件时,我的应用程序“暂停”直到下载完成。我不能点击任何东西,我的装载机不工作等。Phonegap cordova应用得到暂停Android

它用于正常工作,但我不得不将我的应用程序从科尔多瓦1.8.0升级到新版本(2.7.0)。

我也改变了插件本身与新的科尔多瓦合作。

我不知道是什么原因造成的。有任何想法吗?。

-------编辑:添加的代码---------

这里是我下载插件类

public class Downloader extends CordovaPlugin { 

public boolean execute(String action, JSONArray args, CallbackContext callbackContext) { 


    if (!action.equals("downloadFile")){ 
     callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.INVALID_ACTION)); 
     return true; 
    } 
    try { 

     String fileUrl = args.getString(0); 
     JSONObject params = args.getJSONObject(1); 

     String fileName = params.has("fileName") ? 
       params.getString("fileName"): 
       fileUrl.substring(fileUrl.lastIndexOf("/")+1); 

     String dirName = params.has("dirName") ? 
       params.getString("dirName"): 
        Environment.getExternalStorageDirectory().toString(); 

     Boolean overwrite = params.has("overwrite") ? params.getBoolean("overwrite") : false; 

     callbackContext.sendPluginResult(this.downloadUrl(fileUrl, dirName, fileName, overwrite, callbackContext)); 
     return true; 

    } catch (JSONException e) { 

     e.printStackTrace(); 
     callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, e.getMessage())); 
     return false; 

    } catch (InterruptedException e) { 
     e.printStackTrace(); 
     callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, e.getMessage())); 
     return false; 
    } 

} 
private PluginResult downloadUrl(String fileUrl, String dirName, String fileName, Boolean overwrite, CallbackContext callbackContext) throws InterruptedException, JSONException { 


    try { 

     Log.d("PhoneGapLog", "Downloading "+fileUrl + " into " + dirName + "/" + fileName); 

     File dir = new File(dirName); 
     if (!dir.exists()) { 
      Log.d("PhoneGapLog", "directory " + dirName + " created"); 
      dir.mkdirs(); 
     } 

     File file = new File(dirName, fileName); 

     if (!overwrite && file.exists()) { 
      Log.d("DownloaderPlugin", "File already exist"); 

      JSONObject obj = new JSONObject(); 
      obj.put("status", 1); 
      obj.put("total", 0); 
      obj.put("file", fileName); 
      obj.put("dir", dirName); 
      obj.put("progress", 100); 

      return new PluginResult(PluginResult.Status.OK, obj); 
     } 

     URL url = new URL(fileUrl); 
     HttpURLConnection ucon = (HttpURLConnection) url.openConnection(); 
     ucon.setRequestMethod("GET"); 
     ucon.connect(); 

     Log.d("PhoneGapLog", "Download start"); 

     InputStream is = ucon.getInputStream(); 
     byte[] buffer = new byte[1024]; 
     int readed = 0, 
      progress = 0, 
      // totalReaded = 0, 
      fileSize = ucon.getContentLength(); 

     FileOutputStream fos = new FileOutputStream(file); 

     while ((readed = is.read(buffer)) > 0) { 

      fos.write(buffer, 0, readed); 
      //totalReaded += readed; 

      //int newProgress = (int) (totalReaded*100/fileSize);    
      //if (newProgress != progress) 
      // progress = informProgress(fileSize, newProgress, dirName, fileName, callbackId); 

     } 

     fos.close(); 

     Log.d("PhoneGapLog", "Download finished"); 

     JSONObject obj = new JSONObject(); 
     obj.put("status", 1); 
     obj.put("total", fileSize); 
     obj.put("file", fileName); 
     obj.put("dir", dirName); 
     obj.put("progress", progress); 

     return new PluginResult(PluginResult.Status.OK, obj); 


    } 
    catch (FileNotFoundException e) { 
     Log.d("PhoneGapLog", "File Not Found: " + e); 
     return new PluginResult(PluginResult.Status.ERROR, 404); 
    } 
    catch (IOException e) { 
     Log.d("PhoneGapLog", "Error: " + e); 
     return new PluginResult(PluginResult.Status.ERROR, e.getMessage()); 
    } 
} 

}

这是我下载插件的JavaScript

function Downloader() {} 

Downloader.prototype.downloadFile = function(fileUrl, params, win, fail) { 
//Make params hash optional. 
if (!fail) win = params; 
return cordova.exec(win, fail, "Downloader", "downloadFile", [fileUrl, params]); 
}; 

if(!window.plugins) { 
    window.plugins = {}; 
} 
if (!window.plugins.downloader) { 
    window.plugins.downloader = new Downloader(); 
} 

当我把它叫做

$.mobile.showPageLoadingMsg(); 
window.plugins.downloader.downloadFile("URL", 
         {overwrite: true, 
         dirName: dir, fileName: "File.xml"}, 
         function() { 
          alert("finished");  
        }, function(error) { 
         alert("fail"); 
       ); 

是apears因为showPageLoadingMsg的要么冻结,直到下载完成或只有一次下载完成后

显示
+0

你可以发布您的代码 –

+0

代码已添加 –

回答

1

PhoneGap的插件方法被称为对整个应用程序的UI线程,你不应该执行长的装载机活动在UI线程中,否则会被阻止,UI更新甚至整个应用程序都变得不负责任。请阅读文档 - 您应该在单独的线程中执行长时间的任务,将UI线程发布到PhoneGap。它是在2个步骤做 - 你开始一个新的线程,并用标志,这将是一个异步插件调用返回

PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT); 
r.setKeepCallback(true); 
callContext.sendPluginResult(r); 

。当线程完成,完成与呼叫

callContext.success(json); 

编辑:

我错了是科尔多瓦插件的方法被调用在UI线程,他们被称为在WebCore的线程是不一样的用户界面之一(记住是否需要在插件中显示一些用户界面非常重要)。反正你不应该阻止WebCore的线程为好,见文档:

线程

JavaScript中的WebView中确实在UI线程上不运行。它在WebCore线程上运行。执行方法也在WebCore线程上运行。

http://docs.phonegap.com/en/2.2.0/guide_plugin-development_android_index.md.html

相关问题