Android webView ChromeClient onProgress

时间:2013-01-16 11:41:40

标签: android

如何在加载网页时获取资源网址 onProgressChanged(WebView视图,int进度)回调?我需要它 因为从缓存加载时未在已缓存的资源上调用onloadresource。 THX

3 个答案:

答案 0 :(得分:0)

试试这段代码:

final ProgressBar Pbar;
 final TextView txtview = (TextView)findViewById(R.id.tV1);
 Pbar = (ProgressBar) findViewById(R.id.pB1);

MyView.setWebChromeClient(new WebChromeClient() {
      public void onProgressChanged(WebView view, int progress) 
      {
         if(progress < 100 && Pbar.getVisibility() == ProgressBar.GONE){
             Pbar.setVisibility(ProgressBar.VISIBLE);
             txtview.setVisibility(View.VISIBLE);
         }
         Pbar.setProgress(progress);
         if(progress == 100) {
             Pbar.setVisibility(ProgressBar.GONE);
             txtview.setVisibility(View.GONE);
         }
      }
 });

答案 1 :(得分:0)

当你使用WebChromeClient时,你可以覆盖onProgressChanged(WebView view, int progress)此方法用于你的目的,这样的事情可以完成这项工作。

yourWebViewObject.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress)
        {
            currentActivity.setTitle("Loading...");
            currentActivity.setProgress(progress * 100);

            if(progress == 100)
                currentActivity.setTitle(R.string.app_name);
        }
});

对于示例程序,您可以使用Using Android’s WebView, WebChromeClient and WebViewClient to load a webpage and display the progress

答案 2 :(得分:0)

此代码段可能有助于显示中间进度:

 Button btnLoad = (Button) findViewById(R.id.btn_load);
     btnLoad.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
             EditText etUrl = (EditText) findViewById(R.id.et_url);
              mWvUrl.loadUrl(etUrl.getText().toString());
              activity.setProgressBarIndeterminateVisibility(true);
        }
    });

     mWvUrl.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                /** This prevents the loading of pages in system browser */
                return false;
            }

            /** Callback method, executed when the page is completely loaded */
            @Override
            public void onPageFinished(WebView view, String url) {
                 super.onPageFinished(view, url);

                 Toast.makeText(getBaseContext(),
                                "Page loaded Completely",
                                Toast.LENGTH_SHORT).show();

                /** Hiding Indeterminate Progress Bar in the title bar*/
                activity.setProgressBarIndeterminateVisibility(false);

            }

        });