WebView与js交互

main下新建一个文件assets---www---index.html:            布局加一个webview  然后添加一个网络权限

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>hybird</title>
    <script>
        function onJsToNative(){
            var value=document.getElementById("inputValue").value;
            alert(value);
             window.location.href="login://usermanager/userlogin?userName="+value;
        }

        function onJsToNative2(){
           myApp.showToast("from html show on native !");
        }

        function onNativeToJs(fromNativeValue){
            alert(fromNativeValue);
        }

    </script>
</head>
<body>

<img src="https://www.baidu.com/img/bd_logo1.png">
<img src="http://img04.sogoucdn.com/app/a/100520024/4edf482718b95060bbdd6b886248296d">

<input id="inputValue">
<button οnclick="onJsToNative()">jsToNative</button>
<button οnclick="onJsToNative2()">jsToNative2</button>

</body>
</html>
 
 
MianActivity:
public class MainActivity extends AppCompatActivity {
    private com.tencent.smtt.sdk.WebView load;

//    private WebView load;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initData();
    }

    private void initData() {
        load.loadUrl("file:///android_asset/www/index.html");
        WebSettings websetting = load.getSettings();
        websetting.setJavaScriptEnabled(true);
        String ua = websetting.getUserAgentString();
        websetting.setUserAgentString(ua+" bwie");
        load.setWebChromeClient(new WebChromeClient() {

        });
        load.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                if (url.startsWith("login://") && url.contains("userlogin")) {
                    Uri uri = Uri.parse(url);
                    String userName = uri.getQueryParameter("userName");
                    showToast(userName);
                    nativeToHTML();
                    return true;
                }

                return super.shouldOverrideUrlLoading(view, url);
            }
        });
        load.addJavascriptInterface(new MyJavaScriptInter(),"myApp");
    }

    private void initView() {
        load = (com.tencent.smtt.sdk.WebView) findViewById(R.id.webview_mian_loaddatel);
    }

    public void showToast(String str) {
        Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();

    }

    class MyJavaScriptInter {
        @JavascriptInterface
        public void showToast(String str) {
            MainActivity.this.showToast(str);
        }
    }

    public void nativeToHTML() {
        if (Build.VERSION.SDK_INT < 19) {
            load.loadUrl("javascript:onNativeToJs('from native')");
        } else {
            load.evaluateJavascript("javascript:onNativeToJs('from native')", null);
        }

//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            load.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
//        }
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值