在“onCreate”中,我正在从网络下载数据。下载数据的持续时间为 10 秒。我不想在下载数据时使用 ProgressDialog。这是我的代码,但 ProgressDialog 没有出现:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ProgressDialog dialog = ProgressDialog.show(Test.this, "",
"Loading. Please wait...", true);
try {
URL myURL = new URL("http://www.sample.com/");
URLConnection ucon = myURL.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while((current = bis.read()) != -1){
baf.append((byte)current);
}
content= new String(baf.toByteArray());
}
catch (Exception e) {}
dialog.dismiss();
}
最佳答案
执行此类操作的最佳方法是使用 AsyncTask
,以便在下载文件时不会卡住应用程序。此外,如果您通过使用进度条对话框而不是简单对话框来为用户提供更好的体验会怎样。实现起来并不困难;你可以在这里看到一个例子:Download a file with Android, and showing the progress in a ProgressDialog
关于android - ProgressDialog onCreate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3122465/