2013-01-02 86 views
3

的AsyncTask我想這樣做,其進度對話框將等待的WebView加載項。我如何能做到這一點,其dialog.dismiss()事件取決於負載項目上的WebView網頁視圖與Android

public class asynctask extends AsyncTask<Void, Void, Void> { 

    private ProgressDialog dialog = new ProgressDialog(WebActivity.this); 


    @Override 
    protected void onPostExecute(Void result) { 
     // TODO Auto-generated method stub 

在那裏我該怎麼做?

 dialog.dismiss(); 

對話框消失,而無需等待

 } 
@Override 
protected void onPreExecute() { 
     // TODO Auto-generated method stub 
     dialog.setMessage("Loading..Please wait."); 

     dialog.show(); 
     dialog.setCanceledOnTouchOutside(false); 
    } 



@Override 
protected Void doInBackground(Void... params) { 
     // TODO Auto-generated method stub 




     return null; 
    } 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 


    setContentView(R.layout.webview); 

    webView = (WebView) findViewById(R.id.webView1); 


    Bundle extras = getIntent().getExtras(); 
    String url=extras.getString("adres"); 

    webView.setWebViewClient(new WebViewClient()); 
    webView.loadUrl(url); 



    WebSettings webSettings = webView.getSettings(); 
    webSettings.setJavaScriptEnabled(true); 

    new asynctask().execute(); 

}

回答

18

不要使用的AsyncTask,因爲你不負責加載網頁流量的。如果你想顯示一個進度對話框,這裏是如何做到這一點。

private ProgressDialog dialog = new ProgressDialog(WebActivity.this); 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.webview); 
    webView = (WebView) findViewById(R.id.webView1); 

    Bundle extras = getIntent().getExtras(); 
    String url=extras.getString("adres"); 

    webView.setWebViewClient(new WebViewClient() { 
     @Override 
     public void onPageFinished(WebView view, String url) {     
      if (dialog.isShowing()) { 
       dialog.dismiss(); 
      } 
     } 
    }); 
    dialog.setMessage("Loading..Please wait."); 
    dialog.setCanceledOnTouchOutside(false); 
    dialog.show(); 
    webView.loadUrl(url); 



    WebSettings webSettings = webView.getSettings(); 
    webSettings.setJavaScriptEnabled(true); 
} 

這個想法是,你顯示對話框,你開始加載url,當webclient發現頁面已經完成加載時,它會關閉對話框。

+1

感謝分享代碼。 –

+0

重定向怎麼樣? – Skynet