2013-12-20 107 views
5

我想顯示一個進度條顯示完成百分比,而我的應用程序通過互聯網下載一些大文件。 這樣的事情:enter image description here下載時顯示進度條

+0

這是自定義的。 –

+0

你做了這個@Hieu le –

回答

2

xml文件代碼:

<ProgressBar 
     android:id="@+id/xml_reg_progressBar" 
     style="?android:attr/progressBarStyleHorizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="@dimen/common_left_right_margin" 
     android:layout_marginRight="@dimen/common_left_right_margin" 
     android:layout_marginTop="@dimen/reg_ph_linear_top_margin" 
     /> 

你可以改變風格進度按您的要求。

這是用java文件代碼:

protected static final int TIMER_RUNTIME = 180000; // in ms --> 10s 
    private ProgressBar progressTimer; 
    public void startTimer() { 
      final Thread timerThread = new Thread() { 
       @Override 
       public void run() { 
        mbActive = true; 
        try { 
         int waited = 0; 
         while (mbActive && (waited < TIMER_RUNTIME)) { 
          sleep(1000); 
          if (mbActive) { 
           waited += 1000; 

           updateProgress(waited); 
          } 
         } 
        } catch (InterruptedException e) { 
         // do nothing 
        } finally { 
         onContinue(); 
        } 
       } 

      }; 
      timerThread.start(); 

     } 

     Handler handler = new Handler(); 

     private void onContinue() { 
      handler.post(new Runnable() { 
       @Override 
       public void run() { 

        txtCallContactUsNumber 
          .setText(CommonVariable.CallForSupporMessage 
            + contactInfo.MobileNo); 

       } 
      }); 

     } 

     public void updateProgress(final int timePassed) { 
      if (null != progressTimer) { 
       // Ignore rounding error here 
       final int progress = progressTimer.getMax() * timePassed 
         /TIMER_RUNTIME; 
       handler.post(new Runnable() { 
        @Override 
        public void run() { 
         Log.i("timePassed", "timePassed=" + timePassed); 

         DateFormat formatter = new SimpleDateFormat("mm:ss"); 
         Calendar calendar = Calendar.getInstance(); 
         calendar.setTimeInMillis(timePassed); 
         String timer = formatter.format(calendar.getTime()); 
         formatter.setTimeZone(java.util.TimeZone.getTimeZone("GMT")); 
         txtTimerCount.setText(formatter.format(timePassed)); 
        } 
       }); 

       progressTimer.setProgress(progress); 
      } 
     } 

在我的代碼,這將調用1秒,得到進步增量每隔1秒。

+0

感謝您的幫助。 –

+0

歡迎......................... – dipali

+0

感謝它的工作.. –

2

你可以通過兩種方式使用進度條實現此目的。

1.添加進度條(圓圈一)作爲加載指示器。

要做到這一點簡單的顯示一個進度條上的圖像下載線程開始下載完成progressbar.show(); 後駁回進度progressbar.dismiss();

可以使用的AsyncTask這個

請參閱本link

2.添加進度條(矩形欄類型)以顯示下載的進度或可能的情況。

我會建議你去通過這個link ..

0

您應該使用進度條。 我假設你正在使用Asynctask下載文件,如果你不是那麼我建議你這樣做。 異步任務有開的兩個比較爽的方法從

doInBackground()

他們

preExecute和postExecute

可以覆蓋這些方法,現在創建一個簡單的進度條並在preExecute中啓動它,並在postExecute方法中結束它。 這裏是工作LINK

+0

感謝您的幫助。 –