私のアクティビティにはProgressBarがあります。アクティビティを開始するときに、別の場所から取得した値を確認し、ProgressBarに更新します。これが私のコードです:
final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar_detail);
final TextView progressText = (TextView) findViewById(R.id.progressText_detail);
final ImageView btnCancel = (ImageView) findViewById(R.id.imgCancel_detail);
progressBar.setVisibility(View.VISIBLE);
progressText.setVisibility(View.VISIBLE);
btnCancel.setVisibility(View.VISIBLE);
Thread t = new Thread() {
public void run() {
((Activity) ctx).runOnUiThread(new Runnable() {
public void run() {
int oldProgress = 0;
while (progressBar.getProgress() < 100) {
int value = Downloader.progress.get(gameId+ "");
if (value != oldProgress) {
oldProgress = value;
progressBar.setProgress(value);
progressText.setText(value + " %");
}
}
}
});
}
};
t.start();
ProgressBarの値は、私が取得したものint value = Downloader.progress.get(gameId)
であり、正しいものです。しかし、このコードを実行すると、アクティビティが応答せず、何も表示されません(ただし、アプリはクラッシュしません)。実行中のProgressBarを更新し、UIスレッドをブロックして、アクティビティレイアウトが表示されないようにするスレッドのようです。
私のコードの何が問題になっていますか?この状況でProgressBarを更新する正しい方法は何ですか?