Basic internal links don't work in honeycomb app?

前端 未结 2 1197
无人共我
无人共我 2021-01-11 14:16

Internal links do not seem to be working in Android version 3 in my published app. My app targets Froyo at this point.

The app works fine on tons of phones, but my

2条回答
  •  悲&欢浪女
    2021-01-11 15:02

    Here is the solution I use. Works the same on all Android platform (tested on all 1.6 to 4.0.1)

    Load the file as you would normally, without the anchor point. E.g.:

    webview.loadUrl("file:///android_asset/file.html");
    

    Load the URL without the anchor point (e.g. file:///android_asset/file.html), and add:

    webview.setWebViewClient(new WebViewClient()
    {
        public void onPageFinished(WebView view, String url)
        {
            if (this.anchor != null)
            {
                view.loadUrl("javascript:window.location.hash='" + this.anchor + "'");
                this.anchor = null;
            }
        }
    });
    

    where anchor is the anchor point you want to jump to.

    you have to make sure javascript is enabled:

    WebSettings webSettings = webview.getSettings();
    webSettings.setJavaScriptEnabled(true);
    

    This is for loading the file into the webview. Now if you want to catch internal links that are now broken, as suggested in the other answer, override the onReceivedError method and insert do something like:

        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
        {
            int found = failingUrl.indexOf('#');
    
            if (found > 0)
            {
                anchor = failingUrl.substring(found + 1, failingUrl.length());
                view.loadUrl(failingUrl.substring(0, found));
            }
        }
    

提交回复
热议问题