加载网址时是否可以显示进度条?我需要在进度条中显示我可以使用自定义进度条的进度级别。但是我无法找到代码来获取网址加载的确切进度。这是我目前的代码:
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);
}
}
如何获取加载网址的确切进度。使用此代码,我可以获得进度条加载,但它显示为不确定的进度条。我想显示一个自定义视图的进度条。它应该显示网址成功加载的百分比。所以,我可以给定制视图充气,但我再也不知道如何获得确切的百分比。有人可以帮我解决这个问题。
答案 0 :(得分:3)
WebChromeClient的onProgressChanged()为您提供有关页面加载状态的进度。请参阅以下有关android api docs中的函数的信息。
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.
答案 1 :(得分: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.
这是一种虚拟方法。但是否则你应该以某种方式使用Time(秒)。但是,App无法预见打开URL需要多长时间。 100%表示打开URL所需的秒数,但在加载URL时无法访问,因为App当时不知道打开它需要多少。
您可以进行测试运行并计算秒数。当您开始打开URL时设置计时器,并在打开计时器时停止计时器。但那段时间只是在那种特殊情况下..如果有人使用慢速Android设备或糟糕的互联网连接,可能需要更长的时间。
希望我的回答有点有用。如果存在一种更准确地做某事的方法,我相信这里的人会写出来。