WebView js 調用Java本地方法

來源:互聯網
上載者:User

標籤:

webView = (WebView) this.findViewById(R.id.webview);        WebSettings webSettings = webView.getSettings();        webSettings.setJavaScriptEnabled(true);        webView.addJavascriptInterface(new Object(){            /**             * 這個註解,是4.0一下,別人可以隨便調用本地方法。缺少安全性,             * 4.0以上,為了防止別人調用其他的方法。加上了才管用。             * Android4.0以後必須要加,才能調用             * @param str             * @param parentOrigin             */            @JavascriptInterface            public void postMessage(String str, String parentOrigin) {                ToastUtils.showToast(getApplicationContext(), str);            }        }, "parent");                webView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);        // 覆蓋WebView預設使用第三方或系統預設瀏覽器開啟網頁的行為,使網頁用WebView開啟        webView.setWebViewClient(new WebViewClient() {            @Override            public boolean shouldOverrideUrlLoading(WebView view, String url) {                // 傳回值是true的時候控制去WebView開啟,為false調用系統瀏覽器或第三方瀏覽器                view.loadUrl(url);                return true;            }        });

 

WebView js 調用Java本地方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.