Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在加载Webview时显示progressDialog(Android)_Android_Multithreading_Webview - Fatal编程技术网

在加载Webview时显示progressDialog(Android)

在加载Webview时显示progressDialog(Android),android,multithreading,webview,Android,Multithreading,Webview,我加载webview的过程需要一段时间,我想显示一个等待对话框 在过去,我做了以下工作:(但在使用新的Android API时,我得到了错误“在线程上调用了WebView方法”): 显示进度对话框: progressBar=ProgressDialog.show(这个“,”正在加载…) 然后在一个线程中,我加载了webview并向关闭progressDialog的处理程序发送了一条消息: 重新编码后,ProgressDialog不再显示。在Webview.post运行之前,它不会显示 有人

我加载webview的过程需要一段时间,我想显示一个等待对话框

在过去,我做了以下工作:(但在使用新的Android API时,我得到了错误“在线程上调用了WebView方法”):

  • 显示进度对话框:
    progressBar=ProgressDialog.show(这个“,”正在加载…)

  • 然后在一个线程中,我加载了webview并向关闭progressDialog的处理程序发送了一条消息:

重新编码后,ProgressDialog不再显示。在Webview.post运行之前,它不会显示

有人知道我做错了什么吗?有什么帮助吗?

使用下面的方法

    private void startWebView(String url) {
    //Create new webview Client to show progress dialog
    //When opening a url or click on link

    webView.setWebViewClient(new WebViewClient() {      
        ProgressDialog progressDialog;

        //If you will not use this method url links are opeen in new brower not in webview
        public boolean shouldOverrideUrlLoading(WebView view, String url) {              
            view.loadUrl(url);
            return true;
        }

        //Show loader on url load
        public void onLoadResource (WebView view, String url) {
            if (progressDialog == null) {
                // in standard case YourActivity.this
                progressDialog = new ProgressDialog(ShowWebView.this);
                progressDialog.setMessage("Loading...");
                progressDialog.show();
            }
        }
        public void onPageFinished(WebView view, String url) {
            try{
            if (progressDialog.isShowing()) {
                progressDialog.dismiss();
                progressDialog = null;
            }
            }catch(Exception exception){
                exception.printStackTrace();
            }
        }

    }); 

     // Javascript inabled on webview  
    webView.getSettings().setJavaScriptEnabled(true); 

    // Other webview options
    /*
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setUseWideViewPort(true);
    webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    webView.setScrollbarFadingEnabled(false);
    webView.getSettings().setBuiltInZoomControls(true);
    */

    /*
     String summary = "<html><body>You scored <b>192</b> points.</body></html>";
     webview.loadData(summary, "text/html", null); 
     */

    //Load url in webview
    //webView.loadUrl(url);



   //Update1 Start
     wv.post(new Runnable() {
             public void run() {
             String myHTML=generateHTML();
             wv.loadDataWithBaseURL("ImagesPath, myHTML, "text/html", "utf-8", "");
             CloseProgressHandler.sendMessage(mymessage);
               }
            }.start();

    //Update1 End

}
更新2

你可以用这个

Handler handler = new Handler();
handler.postDelayed(new Runnable(){
@Override
      public void run(){

       try{
        if (progressDialog.isShowing()) {
            progressDialog.dismiss();
            progressDialog = null;
        }
        }catch(Exception exception){
            exception.printStackTrace();
        }

   }
}, 5000);

进度将在5秒后取消

非常感谢您的帮助。我想这是正确的方法。但实际上我遇到了麻烦。在您的案例中,什么是ShowWebView?我发现一个语法错误,我知道了。ShowWebView是活动本身。我只是在测试这个。谢谢。将ShowWebView.this替换为您的ActivityName.this它不起作用。我的意思是我可以看到对话框一秒钟,但在页面加载之后。1.单击Load2。页面加载几秒钟3。我可以看到进度对话框一秒钟。对不起,我忘了提什么。我必须用wv.post(如问题中所述的new Runnable())调用webview,否则我会遇到其他问题(“在线程上调用了webview方法”)
    private void startWebView(String url) {
    //Create new webview Client to show progress dialog
    //When opening a url or click on link

    webView.setWebViewClient(new WebViewClient() {      
        ProgressDialog progressDialog;

        //If you will not use this method url links are opeen in new brower not in webview
        public boolean shouldOverrideUrlLoading(WebView view, String url) {              
            view.loadUrl(url);
            return true;
        }

        //Show loader on url load
        public void onLoadResource (WebView view, String url) {
            if (progressDialog == null) {
                // in standard case YourActivity.this
                progressDialog = new ProgressDialog(ShowWebView.this);
                progressDialog.setMessage("Loading...");
                progressDialog.show();
            }
        }
        public void onPageFinished(WebView view, String url) {
            try{
            if (progressDialog.isShowing()) {
                progressDialog.dismiss();
                progressDialog = null;
            }
            }catch(Exception exception){
                exception.printStackTrace();
            }
        }

    }); 

     // Javascript inabled on webview  
    webView.getSettings().setJavaScriptEnabled(true); 

    // Other webview options
    /*
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setUseWideViewPort(true);
    webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    webView.setScrollbarFadingEnabled(false);
    webView.getSettings().setBuiltInZoomControls(true);
    */

    /*
     String summary = "<html><body>You scored <b>192</b> points.</body></html>";
     webview.loadData(summary, "text/html", null); 
     */

    //Load url in webview
    //webView.loadUrl(url);



   //Update1 Start
     wv.post(new Runnable() {
             public void run() {
             String myHTML=generateHTML();
             wv.loadDataWithBaseURL("ImagesPath, myHTML, "text/html", "utf-8", "");
             CloseProgressHandler.sendMessage(mymessage);
               }
            }.start();

    //Update1 End

}
 //Get webview 
    webView = (WebView) findViewById(R.id.webView1);

    startWebView("https://www.google.com/");
Handler handler = new Handler();
handler.postDelayed(new Runnable(){
@Override
      public void run(){

       try{
        if (progressDialog.isShowing()) {
            progressDialog.dismiss();
            progressDialog = null;
        }
        }catch(Exception exception){
            exception.printStackTrace();
        }

   }
}, 5000);