2012-11-17 51 views

回答

2

您可以通過以下方式執行 使用進度欄的關鍵是使用「線程」運行時間消耗任務,並使用另一個「線程」相應地更新進度欄狀態。閱讀代碼的評論,它應該是不言自明的。

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.os.Bundle; 
import android.os.Handler; 
import android.widget.Button; 
import android.view.View; 
import android.view.View.OnClickListener; 

public class MyAndroidAppActivity extends Activity { 

    Button btnStartProgress; 
    ProgressDialog progressBar; 
    private int progressBarStatus = 0; 
    private Handler progressBarHandler = new Handler(); 

    private long fileSize = 0; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     addListenerOnButton(); 

    } 

    public void addListenerOnButton() { 

     btnStartProgress = (Button) findViewById(R.id.btnStartProgress); 
     btnStartProgress.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       // prepare for a progress bar dialog 
       progressBar = new ProgressDialog(v.getContext()); 
       progressBar.setCancelable(true); 
       progressBar.setMessage("File downloading ..."); 
       progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
       progressBar.setProgress(0); 
       progressBar.setMax(100); 
       progressBar.show(); 

       // reset progress bar status 
       progressBarStatus = 0; 

       // reset filesize 
       fileSize = 0; 

       new Thread(new Runnable() { 
        public void run() { 
         while (progressBarStatus < 100) { 

          // process some tasks 
          progressBarStatus = doSomeTasks(); 

          // your computer is too fast, sleep 1 second 
          try { 
           Thread.sleep(1000); 
          } catch (InterruptedException e) { 
           e.printStackTrace(); 
          } 

          // Update the progress bar 
          progressBarHandler.post(new Runnable() { 
           public void run() { 
            progressBar.setProgress(progressBarStatus); 
           } 
          }); 
         } 

         // ok, file is downloaded, 
         if (progressBarStatus >= 100) { 

          // sleep 2 seconds, so that you can see the 100% 
          try { 
           Thread.sleep(2000); 
          } catch (InterruptedException e) { 
           e.printStackTrace(); 
          } 

          // close the progress bar dialog 
          progressBar.dismiss(); 
         } 
        } 
       }).start(); 

      } 

     }); 

    } 

    // file download simulator... a really simple 
    public int doSomeTasks() { 

     while (fileSize <= 1000000) { 

      fileSize++; 

      if (fileSize == 100000) { 
       return 10; 
      } else if (fileSize == 200000) { 
       return 20; 
      } else if (fileSize == 300000) { 
       return 30; 
      } 
      // ...add your own 

     } 

     return 100; 

    } 

} 

打開「佈局/ main.xml中」文件,只需添加普通按鈕進行演示。

<Button 
    android:id="@+id/btnStartProgress" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Download File" /> 

+0

謝謝;我需要把進度條放在通知區域(狀態欄) – Saeed

+1

歡迎,然後你可以使用這段代碼來顯示。 –

+2

您是否計劃在通知區域顯示progressbar,而沒有progrssDialog? – 2012-11-17 07:11:45

1

要顯示通知:

NotificationManager notificationManager = (NotificationManager) mContext 
      .getSystemService(NOTIFICATION_SERVICE); 


     Notification updateComplete = new Notification(); 
     updateComplete.icon = android.R.drawable.stat_notify_sync; 
     updateComplete.tickerText = mContext 
      .getText(R.string.app_name); 
     updateComplete.when = System.currentTimeMillis(); 

     Intent notificationIntent = new Intent(mContext, 
       MainActivity.class); 
      PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, 
       notificationIntent, 0); 

      String contentTitle = mContext.getText(R.string.app_name) 
        .toString(); 
       String contentText; 

        contentText = mContext.getText(
         R.string.yes).toString(); 

       updateComplete.setLatestEventInfo(mContext, contentTitle, 
        contentText, contentIntent); 

       notificationManager.notify(progressBarStatus, updateComplete); 

瞭解更多信息:

http://mobile.tutsplus.com/tutorials/android/android-fundamentals-status-bar-notifications/