Android中调用js方法及js中调用本地方法,有需要的朋友可以参考下。
Android中内置了WebKit模块,而该模块的Java层视图类就是WebView,所有需要使用Web浏览器功能的Android都需要创建该视图类对象显示和处理请求的网络资源。目前WebKit支持Http、Https、Ftp和JavaScript请求。下面我们重点看一下如何在Android中调用JavaScript方法以及如何在js中调用本地方法。
1、在Assets下放一个简单的html文件jstest.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html40/strict.dtd"><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
<?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>
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()"); } }); } }); }}
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(); }}
<uses-permission android:name="android.permission.INTERNET"/>源码在下面:
联系客服