ProgressDialog onCreate [英] ProgressDialog onCreate

查看:137
本文介绍了ProgressDialog onCreate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在"onCreate"中,我正在从Web下载数据.下载数据的持续时间为10秒.数据下载时,我不要有ProgressDialog.这是我的代码,但是没有显示ProgressDialog:

In "onCreate" I'm downloading data from web.The duration of downloading data is 10 sec. I wan't to have ProgressDialog while the data is downloading. Here is my code , but the ProgressDialog doesn't appear:

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,以便在下载文件时不冻结应用程序.此外,如果您使用进度条对话框而不是简单的对话框为用户提供更好的体验,该怎么办?实施起来并不难;您可以在此处看到示例:

The best approach to do this kind of things is using AsyncTask in order to not freeze the application while you are downloading the file. Moreover, what if you give your users a better experience by using a progress bar dialog rather than a simple dialog. It's not difficult to implement; you can see an example here: Download a file with Android, and showing the progress in a ProgressDialog

这篇关于ProgressDialog onCreate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆