2013-06-26 6 views
0

phonegap 애플리케이션 (Android)이 있습니다. xml 파일을 다운로드하려면 플러그인 Downloader을 사용합니다. 파일을 다운로드 할 때 다운로드가 완료 될 때까지 응용 프로그램이 "일시 중지"됩니다. 아무 것도 클릭 할 수 없으며 내 로더가 작동하지 않습니다.Phonegap cordova 앱이 일시 중지되었습니다. Android

잘 작동하는 데 사용되지만 코드가 1.8.0에서 새 버전 (2.7.0)으로 업그레이드해야했습니다.

필자도 새로운 코 도바에서 작동하도록 플러그인 자체를 변경했습니다.

나는이 원인을 알지 못합니다. 어떤 아이디어?

------- 편집 : ADDED CODE --------- 여기

내 다운 플러그인 클래스

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

} 여기

내 다운입니다 자바 스크립트

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

다운로드가 마무리 또는 다운로드가

완료되면에만
+0

당신은 –

+0

코드를 추가 한 코드를 게시 할 수 있습니다 –

답변

1

폰갭 플러그인 방법은 전체 응용 프로그램의 UI 스레드에서 호출, 당신은 오래 수행하지 않아야 표시 될 때까지 동결 하나 때문에 showPageLoadingMsg의 apears 로더 UI 스레드의 활동을 차단합니다. 그렇지 않으면 차단되고 UI가 업데이트되지 않으며 전체 앱도 무책임하게됩니다. 워드 프로세서를 읽으십시오 - 별도의 스레드에서 긴 작업을 수행하여 UI 스레드를 PhoneGap으로 릴리스해야합니다. 이것은 2 단계로 이루어집니다 - 새 스레드를 시작하여 리턴하십시오.

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

플래그는 비동기 플러그인 호출이됩니다. 스레드가 완료되면, 당신과 통화를 마무리

callContext.success(json); 

편집 : 코르도바 플러그인 방법은 UI 스레드에서 호출되는

내가 잘못했다, 그들은 WebCore 스레드에서 호출과 동일하지 않습니다 UI 하나 (플러그인에서 UI를 보여줄 필요가 있는지 기억하는 것이 중요합니다). 어쨌든 당신이뿐만 아니라 WebCore 스레드를 차단 설명서를 참조해서는 안 :

가 웹보기에

자바 스크립트를 스레딩은 UI 스레드에서 하지 실행을 수행합니다. WebCore 스레드에서 실행됩니다. execute 메소드는 WebCore 스레드에서도 실행됩니다.

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

관련 문제