2013-02-11 89 views
0

我有一個按鈕和一個標籤的活動。 按鈕點擊我的應用程序必須下載幾個文件(約9000)。 如果用戶再次點擊按鈕,則必須停止下載,並在另一次點擊時必須從頭開始。如何啓動和停止下載多個文件在android

所以這是我做的:

在活動:

file.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Button b = (Button)v; 
      if(canFile){ 
       b.setText("Stop download"); 
       changeLabelInfo("Getting file list..."); 
       labelFile.setVisibility(View.VISIBLE); 
       fileTask.start(); 
      }else{ 
       b.setText("Download files"); 
       if(fileTask.isAlive()){ 
        fileTask.interrupt(); 
        fileTask = null; 
        fileTask = new UpdateFilesThread(this); 
       } 
       labelFile.setVisibility(View.INVISIBLE); 
       Kernel.setManualUpdate("file",false); 
      } 
      canFile = !canFile; 
     } 
    }); 

必須下載文件的主題是UpdateFilesThread

public class UpdateFilesThread extends Thread{ 
    private MainActivity activity; 
    private final String rootPath = "/mnt/local/"; 
    public UpdateFilesThread(MainActivity activity){ 
     this.activity = activity; 
    } 


    public void run(){ 
     String json = getFilesURL(); 
     JSONObject a = (JSONObject)JSONValue.parse(json); 
     boolean isZip = false,canDownload = true; 
     String[] keys = new String[]{"key1","key2","key3","key4"}; 

     for(String key:keys){ 
      Object folder = (Object)a.get(key); 
      if(folder instanceof JSONObject){ 
       JSONObject fold = (JSONObject)folder; 
       for(Object path_o:fold.keySet()){ 
        path = path_o.toString().replace(" ", "%20"); 
        if(local.endsWith(".php")){ 
         isZip = true; 
         try { 
          Jsoup.connect(mywebserviceURL).data("path",path).timeout(0).post(); // If php generate zip containing php file 
         } catch (IOException e) { 
          canDownload = false; 
         } 
        } 
        if(canDownload){ 
         try{ 
          if(downloadFromUrl(path,isZip)) 
           //SAVE URL DOWNLOADED 
         }catch(Exception e){ 
          e.printStackTrace(); 
         } 
        } 
        canDownload = true; 
        isZip = false; 
       } 
      } 
     } 
     a.remove(key); 
    } 

    private String getFilesURL(){ 
     try { 

      MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
      entity.addPart("type", new StringBody("all")); 
      HttpPost post = new HttpPost("mywebserviceURL"); 
      post.setEntity(entity); 
      HttpClient client = new DefaultHttpClient(); 
      HttpResponse response = client.execute(post); 

      return EntityUtils.toString(response.getEntity()); 
     } catch (UnsupportedEncodingException e) { 
      Support.writeError(e, null); 
      e.printStackTrace(); 
      return ""; 
     } catch (ClientProtocolException e) { 
      Support.writeError(e, null); 
      e.printStackTrace(); 
      return ""; 
     } catch (ParseException e) { 
      Support.writeError(e, null); 
      e.printStackTrace(); 
      return ""; 
     } catch (IOException e) { 
      Support.writeError(e, null); 
      e.printStackTrace(); 
      return ""; 
     } 
    } 
    public boolean downloadFromUrl(String path,boolean isZip){ 
     InputStream is = null; 
     FileOutputStream fos = null; 
     String localFilename = rootPath+path; 
     String local = isZip?rootPath+"tmp.zip":localFilename; 


     boolean return_ = false; 
     try { 
      URL url = new URL(isZip?mywebserviceURLZip:mywebserviceURLZip+path); 
      URLConnection urlConn = url.openConnection(); 
      urlConn.setReadTimeout(0); 
      is = urlConn.getInputStream(); 
      fos = new FileOutputStream(local); 

      byte[] buffer = new byte[51200]; 
      int len; 

      while ((len = is.read(buffer)) > 0) { 
       fos.write(buffer, 0, len); 
      } 
      fos.close(); 
      is.close(); 
      if(isZip){ 
       ZipFile zip = new ZipFile(local); 
       zip.extractAll(rootPath); 
       new File(local).delete(); 
      } 

      return_= true; 
     }catch(Exception e){ 
      e.printStackTrace(); 
      return false; 
     } 
     return return_; 
    } 
} 

當用戶點擊兩個時間我的問題,新生兒按鈕(停止下載並重新開始)。 提示錯誤說該線程已經開始並在運行..我該如何解決它?我知道asyncTask應該更好,但我的問題導致我的應用程序有這麼多的線程運行和設備是如此糟糕的形成.. 有可能停止definitelly線程?有其他更好的解決方案嗎?

回答

0

你運行線程需要偶爾檢查isInterrupted()並退出,如果它返回true。否則,線程將永遠不會被取消。

我覺得你的整個架構是錯的。將9000個文件下載到手機上?即使每個文件只有1KB,這對於移動應用程序來說也是一個巨大的內存。至少你應該將這些數據壓縮並下載一個zip文件,這是爲了你自己的理智。

+0

這是沒有問題的..這個程序將圍繞我市許多平板電腦上運行,需要只執行這個程序...那麼ü有一個更好的架構的提醒? – JackTurky 2013-02-11 16:56:29