我有一门课,我必须在其中执行 3 个长操作。并且必须以 25%、50%、75% 和 100% 的间隔显示进度条。
由于操作使用 UI 线程,我不能将它们放在 DoinBackground 方法()中。
我正在将操作置于 progressUpdate 方法中
代码
package com.integrated.mpr;
import org.apache.commons.math.linear.RealMatrix;
import org.apache.commons.math.stat.correlation.Covariance;
import org.apache.commons.math.stat.correlation.PearsonsCorrelation;
import org.apache.commons.math.util.FastMath;
public class Logic extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.progress);
String x = "abc";
new loadSomeStuff().execute(x);
}
public class loadSomeStuff extends AsyncTask<String,Integer,String>{
ProgressDialog dialog;
protected void onPreExecute(){
dialog = new ProgressDialog(Logic.this);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMax(100);
dialog.show();
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
publishProgress(25);
publishProgress(50);
publishProgress(75);
publishProgress(100);
return null;
}
protected void onProgressUpdate(Integer...progress){
if(progress[0]==25){
dialog.incrementProgressBy(25);
Log.d("now in ", "25 loop");
// do some long work in loop1
dialog.incrementProgressBy(25);
}
else if(progress[0]==25){
dialog.incrementProgressBy(25);
Log.d("now in ", "50 loop");
// do some long work in loop2
dialog.incrementProgressBy(25);
}
else if (progress[0] == 75){
dialog.incrementProgressBy(25);
// do some long work in loop3
}
else{
dialog.incrementProgressBy(25);
}
}
protected void onPostExecute(String result){
dialog.dismiss();
Intent openList = new Intent("com.integrated.mpr.SENSITIVELIST");
startActivity(openList);
}
}
}
现在,当我运行此程序时,会出现一个空白屏幕,并且进度仅显示为 100%。我该如何纠正这个?请帮忙