如何获取用户正在打开或查看的网站网址?

时间:2014-01-02 06:56:28

标签: android

我想创建一个可以获取用户正在打开或查看的网站网址的应用;不仅是浏览器历史记录,我只想获取浏览器页面中的URL。

2 个答案:

答案 0 :(得分:0)

使用WebViewClient获取用户在onPageStarted中打开的url,该代码是否正常工作

WebView webView = (WebView)findViewById(R.id.webView);
WebSettings webset=webView.getSettings();

webset.setJavaScriptEnabled(true);

webView.setWebViewClient(new WebViewClient(){


    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        // TODO Auto-generated method stub
        super.onPageStarted(view, url, favicon);
        Log.i("chauster", "url = "+url);
    }

    @Override
    public void onReceivedHttpAuthRequest(WebView view,
            HttpAuthHandler handler, String host, String realm) {
        // TODO Auto-generated method stub
        //super.onReceivedHttpAuthRequest(view, handler, host, realm);

    }

    @Override
    public void onPageFinished(WebView view, String url) {
        // TODO Auto-generated method stub
        super.onPageFinished(view, url);
    }

    @Override
    public void onReceivedError(WebView view, int errorCode,
            String description, String failingUrl) {
        // TODO Auto-generated method stub

        super.onReceivedError(view, errorCode, description, failingUrl);
    }

    @Override
    public void onReceivedLoginRequest(WebView view, String realm,
            String account, String args) {
        // TODO Auto-generated method stub
        super.onReceivedLoginRequest(view, realm, account, args);
    }

});
webView.loadUrl(Url);

答案 1 :(得分:0)

在你的webView中 -

webView.setWebViewClient(new WebViewClient(){

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        // TODO Auto-generated method stub
        super.onPageStarted(view, url, favicon);

        String webUrl = webView.getUrl(); //gets the current url
        Log.i("chauster", "url = "+url);  //logs the current url
    }
}

见这里:http://developer.android.com/reference/android/webkit/WebView.html#getUrl()