安卓WebView[英] Android WebView

本文是小编为大家收集整理的关于安卓WebView的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

参考此 webview> webview tutorial,特别是此方法

private void setupWebView(){
    String MAP_URL = "http://gmaps-samples.googlecode.com/svn/trunk/articles-android-webmap/simple-android-map.html";
    String centerURL = "javascript:centerAt(" + mostRecentLocation.getLatitude() + ","+ mostRecentLocation.getLongitude()+ ")";
    webView = (WebView) findViewById(R.id.webview);
    webView.getSettings().setJavaScriptEnabled(true);
    //Wait for the page to load then send the location information
    webView.setWebViewClient(new WebViewClient(){
        @Override
        public void onPageFinished(WebView view, String url){
            webView.loadUrl(centerURL);
        }
    });
    webView.loadUrl(MAP_URL);
}

我注意到,如果我直接放置webView.loadUrl(centerURL); webView.loadUrl(MAP_URL);喜欢这个

private void setupWebView(){
    String MAP_URL = "http://gmaps-samples.googlecode.com/svn/trunk/articles-android-webmap/simple-android-map.html";
    String centerURL = "javascript:centerAt(" + mostRecentLocation.getLatitude() + "," + mostRecentLocation.getLongitude()+ ")";
    webView = (WebView) findViewById(R.id.webview);
    webView.getSettings().setJavaScriptEnabled(true);
    //Wait for the page to load then send the location information
    webView.setWebViewClient(new WebViewClient(){
        @Override
        public void onPageFinished(WebView view, String url){
            //DO NOTHING
        }
    });
    webView.loadUrl(MAP_URL);
    webView.loadUrl(centerURL);
}

它不再起作用.因此,centreAt(..) javascript方法包含在MAP_URL.

我想知道webView.loadUrl(..)方法是否在实际加载URL之前返回. 它看起来是这样,因为顶部方法在运行JavaScript

之前等待它完全加载.

推荐答案

是,webView.loadUrl()是异步的:它立即返回,WebView继续在其自己的线程中工作.

监视WebView Page加载使用 WebViewClient.onPageFinished(..) :

webview.setWebViewClient(new WebViewClient() {
    public void onPageFinished(WebView view, String url) {
        // do something here
    }
});

本文地址:https://www.itbaoku.cn/post/102439.html

问题描述

With reference to this WebView tutorial, in particular this method

private void setupWebView(){
    String MAP_URL = "http://gmaps-samples.googlecode.com/svn/trunk/articles-android-webmap/simple-android-map.html";
    String centerURL = "javascript:centerAt(" + mostRecentLocation.getLatitude() + ","+ mostRecentLocation.getLongitude()+ ")";
    webView = (WebView) findViewById(R.id.webview);
    webView.getSettings().setJavaScriptEnabled(true);
    //Wait for the page to load then send the location information
    webView.setWebViewClient(new WebViewClient(){
        @Override
        public void onPageFinished(WebView view, String url){
            webView.loadUrl(centerURL);
        }
    });
    webView.loadUrl(MAP_URL);
}

I've noticed that if I place the webView.loadUrl(centerURL); directly after webView.loadUrl(MAP_URL); like this

private void setupWebView(){
    String MAP_URL = "http://gmaps-samples.googlecode.com/svn/trunk/articles-android-webmap/simple-android-map.html";
    String centerURL = "javascript:centerAt(" + mostRecentLocation.getLatitude() + "," + mostRecentLocation.getLongitude()+ ")";
    webView = (WebView) findViewById(R.id.webview);
    webView.getSettings().setJavaScriptEnabled(true);
    //Wait for the page to load then send the location information
    webView.setWebViewClient(new WebViewClient(){
        @Override
        public void onPageFinished(WebView view, String url){
            //DO NOTHING
        }
    });
    webView.loadUrl(MAP_URL);
    webView.loadUrl(centerURL);
}

it no longer works. So the centreAt(..) javascript method is contained int the MAP_URL.

I'm wondering if the webView.loadUrl(..) method returns before the url has actually been loaded. It looks that way since the top method waits for it to load fully before running the javascript

推荐答案

Yes, webView.loadUrl() is asynchronous: it returns immediately and WebView keeps working in it's own thread.

To monitor WebView page loading use WebViewClient.onPageFinished(..):

webview.setWebViewClient(new WebViewClient() {
    public void onPageFinished(WebView view, String url) {
        // do something here
    }
});