Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
180 views
in Technique[技术] by (71.8m points)

Webview with asynctask on Android

i want to do it which the progress dialog waits the loading item on webview. How can i do it which dialog.dismiss() event depend on loading item on webview?

    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

in there how can i do it ??

      dialog.dismiss();

dialog disappear without waiting

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

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Don't use AsyncTask, as you are not in charge of loading the webview. If you want to show a progress dialog, here is how to do it.

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

The idea is that you show the dialog, you start loading the url, and when the webclient sees that the page has finished loading, it dismisses the dialog.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...