2011-03-09 99 views
0
package com.em.progressb; 

public class progressb extends Activity implements OnClickListener { 
    ProgressDialog dialog; 
    int increment; 

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

     Button startbtn = (Button) findViewById(R.id.startbtn); 
     startbtn.setOnClickListener(this); 
    } 

    public void onClick(View view) { 

     // get the increment value from the text box 
     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 
     int 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 { 
        // enter the code to be run while displaying the progressbar. 
        // 
        // This example is just going to increment the progress bar: 
        // So keep running until the progress value reaches maximum value 
        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 

       } 
      } 
     }); 

     // start the background thread 
     background.start(); 

     progressb o1 = new progressb(); 
     o1.onPause(); 


if(dialog.getProgress()==dialog.getMax()) 
{  
     Intent i = new Intent(this, ShowMyDialog.class); 
     this.startActivity(i); 
} 


    } 

    // handler for the background updating 
    Handler progressHandler = new Handler() { 
     public void handleMessage(Message msg) { 
      dialog.incrementProgressBy(increment); 
     } 
    }; 
} 

這是我第一次activity.it的代碼包含進度條,如進度達到最大極限,我想打電話給第二個活動,我也有寫意圖調用一個活動。 但它不工作。請幫我解決這個問題..Not_Able到呼叫下一個活動

回答

0

移動你的代碼,調用處理程序的下一個活動。你創建的bg線程運行並行到UI線程。

+0

請告訴這是怎麼回事? – OnkarDhane 2011-03-09 05:47:14

+0

正是我在答案中所說的。 *將您的代碼調用處理程序的下一個活動* – Reno 2011-03-09 05:52:19

+0

我已將調用活動代碼移到處理程序,但它給出了此錯誤=構造函數Intent(new Handler(){},Class )未定義 – OnkarDhane 2011-03-09 06:06:11