Android 中任务集的重复 ProgressBar activity
Repeated ProgressBar activity for set of tasks in Android
我是 android 的新手,没有太多管理线程的经验。我正在处理 activity,我想在其中显示进度条 5 秒,然后重复。在那 5 秒内,我将显示一些文本供用户处理文本。我想重复 N 次。
目前,我有以下代码可用于 1 个这样的进度。我尝试循环它但没有帮助,因为线程同时执行。我怎样才能重复 N 次呢?为了解决我的问题,我在正确的道路上吗?
public class test extends Activity {
private ProgressBar progressBar;
private int progressStatus = 0;
private TextView textView;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loop);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
textView = (TextView) findViewById(R.id.textView1);
progressBar.setScaleY(3f);
// Start long running operation in a background thread
for(int i=0; i<5; i++)
Progress();
}
public void Progress(){
new Thread(new Runnable() {
public void run() {
while (progressStatus < 100) {
progressStatus += 1;
// Update the progress bar and display the
//current value in the text view
handler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressStatus);
textView.setText(progressStatus+"/"+progressBar.getMax());
}
});
try {
// Sleep for 200 milliseconds.
//Just to display the progress slowly
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
我不确定我是否会推荐使用您正在遵循的模式,但这里是让线程一个接一个地到达 运行 而不是同时到达的方法:
public void progress(final int numberOfRuns) {
if (numberOfRuns <= 0 ) {
return;
}
new Thread(new Runnable() {
public void run() {
while (progressStatus < 100) {
progressStatus += 1;
// Update the progress bar and display the
//current value in the text view
handler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressStatus);
textView.setText(progressStatus + "/" + progressBar.getMax());
// For the UI Changes. Eg update the loop number
myTextView.setText(Integer.toString(totalLoop));
}
});
try {
// Sleep for 200 milliseconds.
//Just to display the progress slowly
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
progressStatus = 0;
totalLoop = totalLoop+1;
progress(numberOfRuns - 1);
}
}).start();
}
然后只需调用 progress(numberOfRuns)
,无需任何循环。
我是 android 的新手,没有太多管理线程的经验。我正在处理 activity,我想在其中显示进度条 5 秒,然后重复。在那 5 秒内,我将显示一些文本供用户处理文本。我想重复 N 次。
目前,我有以下代码可用于 1 个这样的进度。我尝试循环它但没有帮助,因为线程同时执行。我怎样才能重复 N 次呢?为了解决我的问题,我在正确的道路上吗?
public class test extends Activity {
private ProgressBar progressBar;
private int progressStatus = 0;
private TextView textView;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loop);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
textView = (TextView) findViewById(R.id.textView1);
progressBar.setScaleY(3f);
// Start long running operation in a background thread
for(int i=0; i<5; i++)
Progress();
}
public void Progress(){
new Thread(new Runnable() {
public void run() {
while (progressStatus < 100) {
progressStatus += 1;
// Update the progress bar and display the
//current value in the text view
handler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressStatus);
textView.setText(progressStatus+"/"+progressBar.getMax());
}
});
try {
// Sleep for 200 milliseconds.
//Just to display the progress slowly
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
我不确定我是否会推荐使用您正在遵循的模式,但这里是让线程一个接一个地到达 运行 而不是同时到达的方法:
public void progress(final int numberOfRuns) {
if (numberOfRuns <= 0 ) {
return;
}
new Thread(new Runnable() {
public void run() {
while (progressStatus < 100) {
progressStatus += 1;
// Update the progress bar and display the
//current value in the text view
handler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressStatus);
textView.setText(progressStatus + "/" + progressBar.getMax());
// For the UI Changes. Eg update the loop number
myTextView.setText(Integer.toString(totalLoop));
}
});
try {
// Sleep for 200 milliseconds.
//Just to display the progress slowly
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
progressStatus = 0;
totalLoop = totalLoop+1;
progress(numberOfRuns - 1);
}
}).start();
}
然后只需调用 progress(numberOfRuns)
,无需任何循环。