我尝试设置ProgressDialog以向用户显示InputStream的字节进度。我可以通过以下方式在Android Studio的控制台中显示:
Log.d(TAG, "Progress, transferred " + Integer.toString(100 * bytesTransferred/osSize) + "%");
但是,我不确定如何将bytesTransferred / osSize正确地附加到ProgressDialog,我可以使用一个线程来计算进度条用以下代码填充progressBar所需的时间:
update = new ProgressDialog(this);
update.setTitle("Transferring update");
update.setMessage("Please wait updating...");
update.setCanceledOnTouchOutside(false);
update.setProgress(0);
final int totalProgressTime = 100;
final Thread t = new Thread() {
@Override
public void run() {
int jumpTime = 0;
while(jumpTime < totalProgressTime) {
try {
sleep(1800);
jumpTime += 1;
update.setProgress(jumpTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
t.start();
update.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
update.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
update.show();
}
但是向用户显示字节的实际传输会更好。以下是我必须使用的内容:
final int osSize;
final InputStream inputStream;
osSize = inputStream();
public void onProgress(int bytesTransferred) {
Log.d(TAG, "Progress, transferred " + Integer.toString(100 * bytesTransferred/osSize) + "%");
}
任何帮助将不胜感激,提前谢谢。
答案 0 :(得分:0)
扩展此:
@Override
public void run() {
int jumpTime = 0;
while(jumpTime < totalProgressTime) {
try {
sleep(1800);
jumpTime += 1;
update.setProgress(jumpTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
有了这个:
@Override
public void run() {
int jumpTime = 0;
while(jumpTime < totalProgressTime) {
try {
sleep(1800);
jumpTime += 1;
final int fJumpTime = jumpTime;
runOnUiThread(new Runnable() {
@Override
public void run() {
update.setProgress(fJumpTime);
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}