ProgressDialog onCreate

坚强是说给别人听的谎言 提交于 2019-12-11 04:38:12

问题


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();
}

回答1:


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



来源:https://stackoverflow.com/questions/3122465/progressdialog-oncreate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!