2014-05-21 4 views
1

진행률 표시 줄을 사용하여 다운로드 진행률을 표시하고 있습니다.다운로드하는 동안 ProgressBar가 깜박입니다.

@Override 
    protected String doInBackground(String... f_url) { 
     int count; 

     Looper.prepare(); 
     Boolean check_sd_card_mounted = android.os.Environment 
       .getExternalStorageState().equals(
         android.os.Environment.MEDIA_MOUNTED); 
     ConnectivityManager cm = (ConnectivityManager) mcontext 
       .getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo activeNetworkInfo = cm.getActiveNetworkInfo(); 
     boolean isConnected = false; 
     if (activeNetworkInfo != null) { 
      isConnected = activeNetworkInfo.isConnected(); 
     } 
     if (check_sd_card_mounted) { 
      if (!isConnected) { 
       try { 
        return "no Connection"; 
       } catch (Exception e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } else { 
       download(f_url[0]); 
      } 
     } 

     return null; 
    } 

    /** 
    * Updating progress bar 
    * **/ 
    @Override 
    protected void onProgressUpdate(Integer... values) { 
     // TODO Auto-generated method stub 
     progressBar.setVisibility(View.VISIBLE); 
     progressBar.setIndeterminate(false); 
     progressBar.setMax(100); 
     progressBar.setProgress(values[0]); 

     pinProgress.setProgress(values[0]); 
     updatePinProgressContentDescription(pinProgress); 
    } 

아래에있는 다운로드() 및 복사() 함수있는 내가 진행 막대를

private long download(String url) { 
     int bytesCopied = 0; 
     try { 

      client = new DefaultHttpClient(); 
      httpGet = new HttpGet(url); 
      response = client.execute(httpGet); 
      totalSize = response.getEntity().getContentLength(); 
      File file = new File(Environment.getExternalStorageDirectory() 
        + "/" + "MyFile1.pdf"); 
      if (file.exists() && totalSize == file.length()) { 

       Toast.makeText(mcontext, "File already downloaded", 
         Toast.LENGTH_LONG).show(); 

      } else if (file.exists()) { 
       httpGet.addHeader("Range", "bytes=" + file.length() + "-"); 
       client = new DefaultHttpClient(); 

       httpGet = new HttpGet(url); 
       response = client.execute(httpGet); 
      } 
      outputStream = new ProgressReportingRandomAccessFile(file, "rw"); 
      publishProgress(0, (int) totalSize); 
      InputStream input = response.getEntity().getContent(); 
      bytesCopied = copy(input, outputStream); 

     } catch (Exception e) { 
      Toast.makeText(mcontext, e.getMessage(), Toast.LENGTH_LONG) 
        .show(); 
     } 
     return bytesCopied; 
    } 

    public int copy(InputStream input, RandomAccessFile out) 
      throws IOException, NetworkErrorException { 

     if (input == null || out == null) { 
      return -1; 
     } 

     byte[] buffer = new byte[8192]; 

     BufferedInputStream in = new BufferedInputStream(input, 8192); 

     int count = 0, n = 0; 
     long errorBlockTimePreviousTime = -1, expireTime = 0; 
     long total = 0; 
     try { 

      byte data[] = new byte[8192]; 
      while (!interrupt && (count = in.read(data)) != -1) { 
       out.seek(out.length()); 
       myProgress = (int) (((long) total * 100)/totalSize); 
       publishProgress(myProgress); 
       total += count; 
       // while (!interrupt) { 
       // n = in.read(data, 0, 8192); 
       if (n == -1) { 
        break; 
       } 
       out.write(data, 0, count); 
       // count += n; 
      } 
      IsDownlodComplete = true; 
     } finally { 
      // must close client first 
      client = null; 
      out.close(); 
      in.close(); 
      input.close(); 
     } 
     return count; 

    } 

ProgressBar의 업데이트를 updatemy하는 publishProgress()를 호출하고 있지만 깜박이며 명확하지 - : 여기 내 코드입니다 . UI 스레드에서 실행되지만 여전히 깜박임 onProgressUpdate에서 업데이트하고 있습니다. 이유는 무엇입니까?

미리 감사드립니다. 그에게 onProgressUpdate()을 줄이기 위해

+0

이 progressBar를 listView에 사용하고 있습니다. listView에 설정해야 할 속성이 있습니까? listItem에는 TextView, ProgressBar 및 Button과 같은 많은 뷰가 포함되어 있기 때문입니다. 이것이 깜박 거리는 이유입니까? –

답변

0

시도 :

@Override 
protected void onProgressUpdate(Integer... values) { 
    progressBar.setProgress(values[0]); 
    pinProgress.setProgress(values[0]); 
    updatePinProgressContentDescription(pinProgress); 
} 

onPreExecute()으로 진행 바 초기화 물건을 넣습니다. 초기화를 반복하면 깜박임이 발생합니다.

@Override 
protected void onPreExecute() { 
    progressBar.setVisibility(View.VISIBLE); 
    progressBar.setIndeterminate(false); 
    progressBar.setMax(100); 
} 
+0

이것도 시도해 보았지만, 여전히 깜박입니다. –

+0

listView에서이 progressBar를 사용하고 있습니다. listView에 설정해야 할 속성이 있습니까? listItem에는 TextView, ProgressBar 및 Button과 같은 많은 뷰가 포함되어 있기 때문입니다. 이것이 깜박 거리는 이유입니까? –