确定在android中加载的特定webview页面

时间:2017-09-08 08:25:15

标签: android html webview title

我们在Android的单个WebView类中加载了多个html页面(例如abc.com/abc.html,abc.com/xyz.html)。 现在,我希望实现一旦任何特定页面成功加载,我可以识别特定页面,并代表我可以在本机应用程序中显示一些消息。 我使用webview.getTitle()来识别,但是从webview页面开始有一段时间的变化。

2 个答案:

答案 0 :(得分:0)

您可以在将微粒网页加载到WebView之前比较链接(URL)

webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                progressBar.setVisibility(View.VISIBLE);

              /* compare your URL from here*/

                updateUrl(url);
            }


            @Override
            public void onPageFinished(WebView view, String url) {
//                progressDialog.dismiss();
                progressBar.setVisibility(View.GONE);
            }
        });

答案 1 :(得分:0)

如果这只是您要跟踪的title,则可以实施WebChromeClient并在方法ReceivedTitle

中收听事件ReceivedTitle() 你可以看看: WebChromeClient

myWebView.setWebChromeClient(new WebChromeClient(){
         onReceivedTitle(WebView webview, String title){
             // put your code here

         }
    }
)

修改

  • 您正在处理JavaScript,因此您必须在网络视图中启用它:

    public class MainActivity extends AppCompatActivity {
    
    private WebView myWebView;
    private TextView myTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myWebView = (WebView) findViewById(R.id.webview);
        myWebView.loadUrl("http://example.local/changetitle/");
        myTextView = (TextView) findViewById( R.id.textView);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                myTextView.setText(title);
            }
        });
    }
    

    }

  • 这是一个测试它的示例网页:



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
		<title>First Title</title>
	</head>
	<body onload="myFunction();">
		<div id="text">No changes</div>
		<script>
            var i=27
            function myFunction() {
    
                document.title=""+i;
	            var x = document.getElementById("text");   // because title won't dispay in the body
	            x.innerText = "title : "+document.title;   
	            i++;
	            setTimeout(myFunction,3000);
            }
		</script>

	</body>
</html>
&#13;
&#13;
&#13;

希望有所帮助