Android的ProgressDialog [英] Android ProgressDialog

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

问题描述

我怎么能显示一个ProgressDialog此code?

how can I display a ProgressDialog for this code?

 try{

            leggiNews = Pattern.compile("<p><a href='(.*?)' class='rossobig'>(.*?)</a><br/>(.*?)</p>"); 


            leggi = leggiNews.matcher(getURL("http://www.example.com/"));

                      } catch(UnknownHostException tt){
                             Toast t=Toast.makeText(MainActivity.this, "NESSUNA CONNESSIONE DISPONIBILE!\nATTIVA LA RETE PER QUESTO SERVIZIO.", Toast.LENGTH_LONG);
                             t.setGravity(Gravity.TOP, 0, 240);
                             t.show();
                         }catch (Exception e) {  
                                Toast t=Toast.makeText(MainActivity.this, "[ERRORE GENERICO!]\n"+e, Toast.LENGTH_LONG);
                                 t.setGravity(Gravity.TOP, 0, 240);
                                 t.show();
                } 

专供的getURL()方法:

specifically for the getURL() method:

static final String getURL(String u)throws IOException {
    URL url = new URL(u);
     InputStream content = (InputStream) url.getContent();
      BufferedReader in = new BufferedReader(new InputStreamReader(content));
      String line;
      String a="";
        while ((line = in.readLine()) != null) {
             a+=line; 
             }
      in.close();
      content.close();   
   return a;
   }

该方法的getURL总是在同一个班,我怎么能正确地插入ProgressDialog?谢谢你。

The method getURL is always in the same class, how can I properly insert a ProgressDialog? Thank you.

推荐答案

您可以用不同的线程进行网络运行它,如下所示做

You can do it using different Thread for Network operations as follows

 ProgressDialog progress = ProgressDialog.show(this, "", "Loading ...", true);
 new Thread(new Runnable(){
       @Override
       public void run(){
          try{
                leggiNews = Pattern.compile("<p><a href='(.*?)' class='rossobig'>(.*?)</a><br/>(.*?)</p>"); 
                leggi = leggiNews.matcher(getURL("http://www.example.com/"));
          } catch(UnknownHostException tt){
                         tt.printStackTrace();
          } catch (Exception e) {  
                           e.printStackTrace();
            } 
          runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        progress.dismiss();
                    }
                });

      }
 }).start();

请确保ProgressDialog的那个对象将在这里类变量

Please make sure that object of ProgressDialog will be class variable here

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

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