WebView---Android中调用js方法及js中调用本地方法
16lz
2021-01-23
Android中内置了WebKit模块,而该模块的Java层视图类就是WebView,所有需要使用Web浏览器功能的Android都需要创建该视图类对象显示和处理请求的网络资源。目前WebKit支持Http、Https、Ftp和JavaScript请求。下面我们重点看一下如何在Android中调用JavaScript方法以及如何在js中调用本地方法。
1、在Assets下放一个简单的html文件jstest.html
[html] view plain copy- >
- <HTML>
- <HEAD>
- <meta name="viewport" content="width=device-width, target-densitydpi=device-dpi" />
- <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <script>
- function showMsg(){
- alert("hello world!");
- }
- function showMsgInAndroid(){
- myjs.showMsg('hello in android!');
- }
- script>
- HEAD>
- <BODY>
- <span>测试js使用span>
- <button id='btntest' onclick='showMsgInAndroid()'>调用android方法button>
- BODY>
- HTML>
2、布局文件main.xml
[html] view plain copy
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- android:id="@+id/rl_main"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- xmlns:android="http://schemas.android.com/apk/res/android"
- >
- <WebView
- android:id="@+id/wv_test"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_above="@+id/btn_showmsg"/>
- <Button
- android:id="@+id/btn_showmsg"
- android:layout_width="200dip"
- android:layout_height="40dip"
- android:layout_alignParentBottom="true"
- android:layout_centerHorizontal="true"
- android:text="调用html中js方法"/>
- RelativeLayout>
3、然后是Activity,MainActivity.java
[java] view plain copy
- package com.harold.jstest;
- import com.harold.base.JSKit;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Handler;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.webkit.WebChromeClient;
- import android.webkit.WebView;
- import android.widget.Button;
- public class MainActivity extends Activity {
- private WebView mWebView;
- private Button btnShowInfo;
- private JSKit js;
- private Handler mHandler = new Handler();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //初始化控件
- mWebView = (WebView) findViewById(R.id.wv_test);
- btnShowInfo = (Button) findViewById(R.id.btn_showmsg);
- //实例化js对象
- js = new JSKit(this);
- //设置参数
- mWebView.getSettings().setBuiltInZoomControls(true);
- //内容的渲染需要webviewChromClient去实现,设置webviewChromClient基类,解决js中alert不弹出的问题和其他内容渲染问题
- mWebView.setWebChromeClient(new WebChromeClient());
- mWebView.getSettings().setJavaScriptEnabled(true);
- //把js绑定到全局的myjs上,myjs的作用域是全局的,初始化后可随处使用
- mWebView.addJavascriptInterface(js, "myjs");
- mWebView.loadUrl("file:///android_asset/jstest.html");
- btnShowInfo.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- mHandler.post(new Runnable() {
- @Override
- public void run() {
- //调用 HTML 中的javaScript 函数
- mWebView.loadUrl("javascript:showMsg()");
- }
- });
- }
- });
- }
- }
4、最后是绑定全局js的类 JSKit.java
[java] view plain copy
- package com.harold.base;
- import android.widget.Toast;
- import com.harold.jstest.MainActivity;
- public class JSKit {
- private MainActivity ma;
- public JSKit(MainActivity context) {
- this.ma = context;
- }
- public void showMsg(String msg) {
- Toast.makeText(ma, msg, Toast.LENGTH_SHORT).show();
- }
- }
例子比较简单,代码里都加了注释,这里就不多说了,本示例用的本地的html,如果访问网络中的网页,别忘记在AndroidManifest.xml中加权限
[html] view plain copy
- <uses-permission android:name="android.permission.INTERNET"/>
------> 源码下载<------
更多相关文章
- Linux下的Android电话管理软件――QtADB(安装启动方法)
- Android:绘制自定义视图
- Android adb功能使用方法
- Android 4.4 全套源码及子模块源码的下载方法
- android解析XML文件的三方法之SAX
- android 使用Application保存全局变量
- Android 4.4 Dialog 被状态栏遮挡的解决方法