android js webview 互调

<html lang='zh-CN' xml:lang='zh-CN' xmlns='http://www.w3.org/1999/xhtml'>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

    <link rel="shortcut icon" type="image/x-icon" href="/img/favicon.ico?t=1451964198000" />
    <title>android studio 如何读取html文件的?如何存放原生资源? - 开源中国社区</title>
    <meta name="Description" itemprop="description" content="android studio 如何读取html文件的?如何存放原生资源?- 开源中国社区"/>

</head>
<body>
<p>WebView与Javascript交互</p>
<div>
    <button onClick="window.test.actionFromJs()">点击调用Native代码</button>
</div>
<br/>
<div>
    <button onClick="window.test.actionFromJsWithParam('come from Js')">点击调用Native代码并传递参数</button>
</div>
<br/>
<div id="log_msg">调用打印信息</div>

</body>

</html>

/*
**测试webview和 js的互调
 */
public class TestActivity extends BaseActivity {

    private WebView wv_test;
    private Button btn_test;
    private TextView logTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test2);
        wv_test = (WebView) findViewById(R.id.wv_test);
        btn_test = (Button)findViewById(R.id.btn_test);
        logTextView = (TextView) findViewById(R.id.logTextView);
        getData();
    }


    private void getData(){
        // 设置支持javascript
        wv_test.getSettings().setJavaScriptEnabled(true);
        //启动缓存
        wv_test.getSettings().setAppCacheEnabled(true);
        //设置缓存模式
        wv_test.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        Log.e("error"," wv_test.loadUrl(file:///android_asset/index.html)");
        //加载网页
        wv_test.loadUrl("file:///android_asset/index.html");
        wv_test.addJavascriptInterface(this, "test");
        btn_test.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                // 无参数调用
                wv_test.loadUrl("javascript:actionFromNative()");
                // 传递参数调用
                wv_test.loadUrl("javascript:actionFromNativeWithParam(" + "'come from Native'" + ")");
            }
        });

    }

    @android.webkit.JavascriptInterface
    public void actionFromJs() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(TestActivity.this, "js调用了Native函数", Toast.LENGTH_SHORT).show();
                String text = logTextView.getText() + "\njs调用了Native函数";
                logTextView.setText(text);
            }
        });
    }

    @android.webkit.JavascriptInterface
    public void actionFromJsWithParam(final String str) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(TestActivity.this, "js调用了Native函数传递参数:" + str, Toast.LENGTH_SHORT).show();
                String text = logTextView.getText() + "\njs调用了Native函数传递参数:" + str;
                logTextView.setText(text);
            }
        });

    }
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
   >
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
<Button
    android:id="@+id/btn_test"
    android:text="访问"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
    <TextView
        android:text="log"
        android:layout_marginLeft="20dp"
        android:layout_marginStart="20dp"
        android:textSize="24sp"
        android:id="@+id/logTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" /></LinearLayout>
<WebView
    android:id="@+id/wv_test"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
</LinearLayout>
源码链接  https://github.com/letuszou/WebviewJs

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值