2012-01-31 91 views
1

我嘗試使用以下代碼加載URL。如何顯示加載對話框

URL url = new URL(urlstr); 
HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 
connection.setConnectTimeout(10000); 
connection.setDoInput(true); 
connection.setDoOutput(true); 
connection.setUseCaches(false); 
InputStream is = connection.getInputStream(); //spend lots of time 

因爲線路InputStream is = connection.getInputStream();會花一些時間。 所以我想在加載時顯示一個加載對話框。 我能做到嗎?

在AActivity中,下面的代碼調用BActivity。

Intent intent = new Intent(AActivity.this, BActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
Window w = MyGroup.group.getLocalActivityManager().startActivity("BActivity", intent); 
View view = w.getDecorView(); 
MyGroup.group.setContentView(view); 

而BActivity是加載URL和提取信息。 加載代碼在onCreate()中。

我嘗試了答案代碼,錯誤Unable to add window -- token [email protected] is not valid; is your activity running?節目。

回答

3

您可以顯示進度對話框aysntask實現如下

在OnCreate中:

  new GetTask(this).execute();//taken object for asyntask class. 



    class GetTask extends AsyncTask<Object, Void, String> { 
    { 
     ProgressDialog progressDialog; 
     void GetTask(Context cntxt) 
     { 

     } 
     @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 

     mDialog = new ProgressDialog(cntxt); //taking object for progress dialog 
     mDialog.setMessage("Please wait..."); 
     mDialog.show(); //Displaying progressDialog 
    } 

    @Override 
    protected String doInBackground(Object... params) { 

     //do the background process 

     return ""; you can return string value 

    } 

    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 

     if (mDialog != null) { 
      mDialog.dismiss();//close the progress Dialog 
     } 

    } 
     } 
+0

我覺得應該用 如果(mDialog.isShowing()){ mDialog.dismiss(); //關閉進度對話框 } 代替 如果(支持mDialog!= NULL){ 支持mDialog。駁回(); //關閉進度對話框 } – Akram 2012-01-31 05:29:54

+0

「無法添加窗口」的消息顯示。我編輯了我的問題。 – brian 2012-01-31 05:44:03

1

進度對話框

private ProgressDialog dialog; 
    public void showProgress() { 
     dialog = new ProgressDialog(this); 
     dialog.setCancelable(true); 
     dialog.setMessage("Please wait"); 
     dialog.show(); 

    } 

使用異步任務downloding ...

private class DownloadWebPageTask extends AsyncTask<String, Void, String> { 
     @Override 
     protected String doInBackground(String... urls) { 
      String response = ""; 
      for (String url : urls) { 
       //Do your downloading task 

       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
      return response; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
       dialog.dismiss(); 
      } 
     } 

呼叫進度對話框執行下載任務

showProgress(); 
     DownloadWebPageTask task = new DownloadWebPageTask(); 
     task.execute(new String[] { "http://www.url.com" }); 
+0

「無法添加窗口」消息顯示。我編輯了我的問題。 – brian 2012-01-31 05:44:12

+0

請更換'this'用'YourActivityName.this' – Sandy 2012-01-31 05:51:07

+0

它仍然沒有工作。 – brian 2012-01-31 05:52:50

1

使用DownloadWebPageTask構造函數初始化上下文和使用方面的對話。

或使用yourclass.this在 對話框=新ProgressDialog(yourclass.this);