android - ProgressDialog onCreate

标签 android progressdialog oncreate

在“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/

相关文章:

android - OpenGL ES 2.0——简单粒子系统的最佳途径

Android KeyBoard 不显示在 AlertDialog 中

java - Android - 具有形状可绘制对象和以编程方式渐变的自定义按钮

android 如何使用 progressdialog

android - Android-应用程序崩溃

android - OnLongClickListener 没有正常工作

java - 实时过滤输入?

Android:ProgressDialog.show() WindowManager token ,无效

swing - 非阻塞模态 Swing 进度对话框

android - 屏幕方向更改后,dialogFragment 显然没有任何调用出现