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);
}
};
}
这是我的第一个活动的代码。它包含进度条,因为进度达到最大限制我想调用第二个活动,并且我也有书面意图调用下一个活动。 但它不起作用。请帮助我解决这个问题。
答案 0 :(得分:0)
将调用下一个活动的代码移动到处理程序。您创建的bg线程将 parallel 运行到UI线程。