2011-03-18 13 views
0
public class ChildLock extends Activity implements OnClickListener 
{ 
    ProgressDialog dialog; 
    int increment; 
    int maximum ; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Button startbtn = (Button) findViewById(R.id.startbtn); 
     startbtn.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View arg0) 
    { 
    EditText et = (EditText) findViewById(R.id.increment); 
     // convert the text value to a integer 
     increment = Integer.parseInt(et.getText().toString()); 

     dialog = new ProgressDialog(this); 
     dialog.setCancelable(true); 
     dialog.setMessage("Loading..."); 
     // set the progress to be horizontal 
     dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
     // reset the bar to the default value of 0 
     dialog.setProgress(0); 

     // get the maximum value 
     EditText max = (EditText) findViewById(R.id.maximum); 
     // convert the text value to a integer 
      maximum = Integer.parseInt(max.getText().toString()); 
     // set the maximum value 
     dialog.setMax(maximum); 
     // display the progressbar 
     dialog.show(); 

     // create a thread for updating the progress bar 
     Thread background = new Thread (new Runnable() { 
      public void run() 
      { 
       try 
       { 
        while(dialog.getProgress()<= dialog.getMax()) 
        { 
         // wait 500ms between each update 
         Thread.sleep(500); 
         // active the update handler 
         progressHandler.sendMessage(progressHandler.obtainMessage()); 
        } 
      } 
       catch (java.lang.InterruptedException e) 
       { 
        // if something fails do something smart 
       } 
      } 
     }); 
     background.start(); 
    } 

    // handler for the background updating 
    Handler progressHandler = new Handler() 
    { 
     public void handleMessage(Message msg) 
     { 
      if(dialog.getProgress()== dialog.getMax()) 
      { 
       Intent i = new Intent(ChildLock.this, notifi.class); 
        startActivity(i); 

      } 
      dialog.incrementProgressBy(increment); 
     } 

    }; 

} 

回答

0

,如果你正在運行的線程通常會發生如下 ,那麼你必須通過的消息給UI線程這個線程

 public class MyActivity extends Activity { 
      private static final int PROGRESS = 0x1; 

      private ProgressBar mProgress; 
      private int mProgressStatus = 0; 

     private Handler mHandler = new Handler(); 

     protected void onCreate(Bundle icicle) { 
      super.onCreate(icicle); 

      setContentView(R.layout.progressbar_activity); 

      mProgress = (ProgressBar) findViewById(R.id.progress_bar); 

      // Start lengthy operation in a background thread 
      new Thread(new Runnable() { 
       public void run() { 
        while (mProgressStatus < 100) { 
         mProgressStatus = doWork(); 
          mProgress.setProgress(mProgressStatus); 
         } 
         //pass messege to UI thread 
         Message msg=new Message(); 
         msg.obj="your text"; 
         mHandler.sendMessage(msg); 

         }); 
        } 
       } 
      }).start(); 
     } 
    } 

和你mHandler會是這樣的通知

你的進度條
mHandler = new Handler() { 
       @Override public void handleMessage(Message msg) { 
       String i=(String)msg.obj; 
       Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); 
       } 
      }; 

粘貼此代碼後進度線程 while循環

while(dialog.getProgress()<= dialog.getMax()){ 
    .......... 
    } 

Message msg=new Message(); 
msg.obj="your text"; 
mHandler.sendMessage(msg); 

,並在創建的onCreate mHandler()方法

+0

@sunil我已經編輯我的問題:我的進度條code..please看到,幫我整合有你的想法...在此先感謝 – Smith 2011-03-18 09:46:46

+0

PLZ檢查可能有幫助 – 2011-03-18 10:02:24

+0

@sunil我應該爲通知消息框寫新類還是直接寫入代碼進行處理程序...... ??? – Smith 2011-03-18 10:03:20