2014-12-05 95 views
2

加载网址时可以显示进度条吗?我需要显示进度条中的进度级别,我可以使用自定义进度条。但我无法找到代码来获取他已加载的网址的确切进度。这是我当前的代码:加载网址到WebView时显示ProgressBar

public class MainActivity extends ActionBarActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    browser = (WebView)findViewById(R.id.webView1); 
    progressBar = (ProgressBar)findViewById(R.id.progressBar); 
    browser.setWebViewClient(new MyBrowser()); 
    browser.setWebChromeClient(new MyCustomChromeClient()); 
    mContext=this.getApplicationContext(); 
    browser.loadUrl(target_url); 
    MainActivity.this.progressBar.setProgress(0); 
} 

private class MyBrowser extends WebViewClient { 

    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     String host = Uri.parse(url).getHost(); 
     if (host.equals(target_url_prefix)) 
     { 
      if(mWebviewPop!=null) 
      { 
       mWebviewPop.setVisibility(View.GONE); 
       baseLayout.removeView(mWebviewPop); 
       mWebviewPop=null; 
      } 
      return false; 
     } 

     if(host.equals("m.facebook.com")) 
     { 
      return false; 
     } 
     view.loadUrl(url); 
     return true; 
    } 

    @Override 
    public void onReceivedError(WebView view, int errorCode, 
           String description, String failingUrl) { 
     noInternet(); 
    } 

    @Override 
    public void onPageStarted(WebView view, String url, Bitmap favicon) { 
     visible(); 
    } 

    @Override 
    public void onPageFinished(WebView view, String url) { 
     unvisible(); 
     System.out.println("\n" +view.getUrl()); 
     if(url.startsWith("https://m.facebook.com/v2.1/dialog/oauth")){ 
      if(mWebviewPop!=null) 
      { 
       mWebviewPop.setVisibility(View.GONE); 
       baseLayout.removeView(mWebviewPop); 
       mWebviewPop=null; 
      } 
      view.loadUrl(redirectUrl); 
      return; 
     } 
     super.onPageFinished(view, url); 
    } 
} 

private class MyCustomChromeClient extends WebChromeClient 
{ 

    @Override 
    public boolean onCreateWindow(WebView view, boolean isDialog, 
            boolean isUserGesture, Message resultMsg) { 
     mWebviewPop = new WebView(mContext); 
     mWebviewPop.setVerticalScrollBarEnabled(false); 
     mWebviewPop.setHorizontalScrollBarEnabled(false); 
     mWebviewPop.setWebViewClient(new MyBrowser()); 
     mWebviewPop.getSettings().setJavaScriptEnabled(true); 
     mWebviewPop.getSettings().setSavePassword(false); 
     mWebviewPop.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
       ViewGroup.LayoutParams.MATCH_PARENT)); 
     baseLayout.addView(mWebviewPop); 
     WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj; 
     transport.setWebView(mWebviewPop); 
     resultMsg.sendToTarget(); 

     return true; 
    } 

    @Override 
    public void onCloseWindow(WebView window) { 
    } 

    @Override 
    public void onProgressChanged(WebView view, int newProgress) { 
     MainActivity.this.setValue(newProgress); 
     super.onProgressChanged(view, newProgress); 
    } 
} 
public void setValue(int progress) { 
    this.progressBar.setProgress(progress); 
} 
} 

如何获取加载url的确切进度。有了这段代码,我可以得到进度条加载,但它显示为一个不确定的进度条。我想显示一个自定义视图的进度条。它应该显示url成功加载的百分比。所以我可以膨胀一个自定义的视图,但我不知道如何得到确切的百分比。有人可以帮助我使用此代码。

回答

3

WebChromeClient的onProgressChanged()为您提供有关页面加载状态的进度。请参阅以下有关android api文档中的函数的信息。

public void onProgressChanged (WebView view, int newProgress) 

Added in API level 1 
Tell the host application the current progress of loading a page. 

Parameters 
view The WebView that initiated the callback. 
newProgress Current page loading progress, represented by an integer between 0 and 100. 
+0

谢谢:)正是我需要的答案:) – 2014-12-05 15:09:27

1

我不熟悉你的Webview。但是我在doInBackground AsyncTask ussualy中打开了URL。所以我找不到直接用百分比表示的方法。但你可以做的也许是欺骗,像这样的东西:

Set 20% when your app has confirmed that there is a INTERNET connection enables on the Device. 
Set 50% when it reaches the AsyncTask to open URL (or however you initialize .connect()) 
And finally set 100% when URL is opened. 

这是一个虚拟的方法。但除此之外,你应该以某种方式使用时间(秒)。但是,应用程序无法预见它需要打开URL多长时间。 100%代表打开URL所需的秒数,但在加载URL时无法访问,因为App当时不知道需要打开它多少。

你可以做一个测试运行和时间秒。设置一个计时器,当你开始打开URL,并打开它时停止计时器。但那段时间只是在这种特殊情况下。如果有人使用慢速Android设备或Bad Internet Connection,则可能需要更长的时间。

希望我的回答有所帮助。如果存在一种更精确的方法,我相信这里的人会写出来。

+0

谢谢:)我也想到了定时器。但是在不同的设备中可能会有所不同。感谢您的想法。 – 2014-12-05 15:06:35